React Custom Hook: useDebounce
JavaScriptDebounce a fast-changing value, e.g. a search box.
JavaScript · 8 lines
copied 38 times
function useDebounce(value, delay) {
const [debounced, setDebounced] = useState(value);
useEffect(() => {
const t = setTimeout(() => setDebounced(value), delay);
return () => clearTimeout(t);
}, [value, delay]);
return debounced;
}
1
Fire
0
Learned something
1
Saved me time
1
Mind-blown
Sign in to react and star