Overview
usePeliculaSearch is a custom React hook that provides search and category filtering functionality for the movie catalog. It manages search state, filtered results, loading states, and available categories.
Import
Signature
Initial array of movie objects to filter. Defaults to the complete
mockPeliculas dataset.Return Value
The hook returns an object with the following properties:Current search query string
State setter function to update the search termSignature:
(term: string) => voidCurrently selected genre/category filter (empty string means all categories)
State setter function to update the selected categorySignature:
(category: string) => voidArray of movie objects that match the current search and category filters
Loading state indicator (true while filtering is in progress)
Array of unique genre strings extracted from all movies
Behavior
Search Functionality
The search filters movies by matching thesearchTerm (case-insensitive) against:
- Movie title (
pelicula.title) - Director name (
pelicula.director) - Cast members (
pelicula.castarray)
Category Filtering
When a category is selected, only movies with matchinggenre values are returned.
Debouncing
Filtering operations are debounced with a 300ms delay to simulate network latency and prevent excessive re-renders.Dependencies
The filtering effect runs wheneversearchTerm or selectedCategory changes.
Usage Example
Implementation Details
State Management
The hook maintains four internal state variables:searchTerm: String input from search barselectedCategory: String for genre filterfilteredPeliculas: Computed array of matching moviesloading: Boolean for UI feedback
Filter Algorithm
Source Location
src/hooks/usePeliculaSearch.js:4