Skip to content

Commit

Permalink
Merge Merge pull request #232 from debouncing-effect-search
Browse files Browse the repository at this point in the history
Added debouncing effect
  • Loading branch information
github-actions[bot] authored Oct 10, 2024
2 parents ab7aaaa + 0c79d24 commit 85d8105
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/components/Search.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,30 @@ export default function Search({ movies}) {
const navigate = useNavigate();
const [searchVal, setSearchVal] = useState("");
const [filteredMovies, setFilteredMovies] = useState([]);
const [debouncedSearchVal, setDebouncedSearchVal] = useState(searchVal);

const handleSearchVal = (e) => {
setSearchVal(e.target.value);
};

// Debouncing logic: Updates debouncedSearchVal after a delay
useEffect(() => {
const handler = setTimeout(() => {
setDebouncedSearchVal(searchVal);
}, 700); // Adjust the delay as needed (1000ms here)

// Cleanup function to clear timeout if the component re-renders before the delay ends
return () => {
clearTimeout(handler);
};
}, [searchVal]);

useEffect(() => {
const filtered = movies.filter(movie =>
movie?.movieTitle?.toLowerCase().includes(searchVal.toLowerCase())
movie?.movieTitle?.toLowerCase().includes(debouncedSearchVal.toLowerCase())
);
setFilteredMovies(filtered);
}, [searchVal, movies]);
}, [debouncedSearchVal, movies]);

return (
<div className='search-wrapper'>
Expand Down

0 comments on commit 85d8105

Please sign in to comment.