useDebounce
TheuseDebounce hook delays updating a value until after a specified amount of time has passed since the last change. This is useful for optimizing performance in scenarios like search inputs, where you want to wait for the user to stop typing before making an API call.
Installation
Import
Usage
Parameters
T
required
The value to debounce. Can be of any type.
number
default:500
The delay in milliseconds before the value is updated.
Returns
T
The debounced value that updates only after the specified delay has passed since the last change to the input value.
Type Definition
Examples
Search with API Calls
Form Validation
Auto-save Feature
Window Resize Handler
Filter with Multiple Criteria
Common Patterns
Custom Delay Based on Input Length
Combining with Loading State
Notes
- The debounced value will be the same as the input value on the initial render
- The timer is reset every time the input value changes
- The cleanup function ensures the timeout is cleared when the component unmounts or when the value changes
- This hook is useful for reducing API calls, optimizing re-renders, and improving performance
- Consider using a shorter delay (200-300ms) for better user experience in search interfaces
- For very expensive operations, a longer delay (500-1000ms) may be appropriate