useSearch Hook Logic - abhiram-shaji/Langroove GitHub Wiki

Developer Documentation: useSearch Hook

This documentation provides an overview of the useSearch hook, which implements a reusable search functionality to filter a dataset based on user input.


Overview

The useSearch hook:

  1. Maintains a search term state.
  2. Filters a dataset based on the search term.
  3. Returns the search term, its setter, and the filtered dataset.

Features and Functionality

1. State Management

Purpose

  • Manages the search term entered by the user.

Implementation

  1. State Variable:

    • search: Stores the current search term.
  2. Code Snippet:

    const [search, setSearch] = useState<string>('');
  3. Process:

    • The setSearch function is used to update the search term dynamically.

2. Filtering Data

Purpose

  • Filters the input dataset based on the current search term.

Implementation

  1. Functionality:

    • Converts the search term and item names to lowercase for a case-insensitive match.
    • Filters the dataset by checking if each item's name includes the search term.
  2. Code Snippet:

    const filteredData = data.filter((item) =>
      item.name.toLowerCase().includes(search.toLowerCase())
    );
  3. Process:

    • Iterates through the data array to filter items.
    • Updates the filteredData array dynamically as the search term changes.

TypeScript Integration

Generics

Purpose

  • Ensures reusability across datasets with different types, as long as the items include a name field.

Implementation

  1. Generic Type:

    • T represents the type of each item in the dataset.
    • Extends { name: string } to ensure items have a name field.
  2. Code Snippet:

    const useSearch = <T extends { name: string }>(data: T[]): UseSearch<T> => { ... };
  3. Benefits:

    • Provides compile-time type checking.
    • Ensures the hook works with various data structures that include a name field.

Key Properties and Methods

Property/Method Description
search The current search term.
setSearch(value) Updates the search term dynamically.
filteredData The dataset filtered based on the search term.

Usage Example

Dataset

const items = [
  { name: 'Apple', category: 'Fruit' },
  { name: 'Banana', category: 'Fruit' },
  { name: 'Carrot', category: 'Vegetable' },
];

Hook Usage

import useSearch from './hooks/useSearch';

const App = () => {
  const { search, setSearch, filteredData } = useSearch(items);

  return (
    <div>
      <input
        type="text"
        value={search}
        onChange={(e) => setSearch(e.target.value)}
        placeholder="Search items..."
      />
      <ul>
        {filteredData.map((item, index) => (
          <li key={index}>{item.name}</li>
        ))}
      </ul>
    </div>
  );
};

Error Handling

  • Empty Search Term:

    • If the search term is empty, the filteredData array returns the original dataset.
  • Case Insensitivity:

    • Converts both search term and item names to lowercase to handle case differences.

Notes

Reusability

  • The hook is generic and can be reused for any dataset that contains a name field, regardless of additional properties.

Performance

  • For large datasets, consider optimizing filtering logic or implementing debounce to improve performance.

For additional customization, refer to the React Documentation.

⚠️ **GitHub.com Fallback** ⚠️