Custom hook that debounces a value by a specified delay.
This hook takes a value and a delay, and returns a debounced version of the value. The debounced value only updates after the specified delay has passed without changes to the input value.
The value to be debounced.
The delay in milliseconds for debouncing.
const [searchTerm, setSearchTerm] = useState('');const debouncedSearchTerm = useDebounce(searchTerm, 500);useEffect(() => { if (debouncedSearchTerm) { // Perform search operation console.log('Searching for:', debouncedSearchTerm); }}, [debouncedSearchTerm]); Copy
const [searchTerm, setSearchTerm] = useState('');const debouncedSearchTerm = useDebounce(searchTerm, 500);useEffect(() => { if (debouncedSearchTerm) { // Perform search operation console.log('Searching for:', debouncedSearchTerm); }}, [debouncedSearchTerm]);
Custom hook that debounces a value by a specified delay.
This hook takes a value and a delay, and returns a debounced version of the value. The debounced value only updates after the specified delay has passed without changes to the input value.