Introduction
Delay updating a value until the user stops typing (or until a timer elapses). Handy for search inputs and autosave.
Platform: React (web and native). Dependencies: react only.
Hook
For React Native, replace window.setTimeout / window.clearTimeout with setTimeout / clearTimeout from the global scope.
Example
tsx
const [query, setQuery] = useState("");const debouncedQuery = useDebounce(query, 400);useEffect(() => {if (!debouncedQuery) return;fetchResults(debouncedQuery);}, [debouncedQuery]);
Footer
Tune delayMs per UX: search fields often use 250–500 ms.