1.3 KiB
1.3 KiB
title, type, language, tags, author, cover, dateModified
| title | type | language | tags | author | cover | dateModified | |||
|---|---|---|---|---|---|---|---|---|---|
| React useDebounce hook | snippet | react |
|
chalarangelo | blue-bird | 2021-01-04T16:48:43+02:00 |
Debounces the given value.
- Create a custom hook that takes a
valueand adelay. - Use the
useState()hook to store the debounced value. - Use the
useEffect()hook to update the debounced value every timevalueis updated. - Use
setTimeout()to create a timeout that delays invoking the setter of the previous state variable bydelayms. - Use
clearTimeout()to clean up when dismounting the component. - This is particularly useful when dealing with user input.
const useDebounce = (value, delay) => {
const [debouncedValue, setDebouncedValue] = React.useState(value);
React.useEffect(() => {
const handler = setTimeout(() => {
setDebouncedValue(value);
}, delay);
return () => {
clearTimeout(handler);
};
}, [value]);
return debouncedValue;
};
const Counter = () => {
const [value, setValue] = React.useState(0);
const lastValue = useDebounce(value, 500);
return (
<div>
<p>
Current: {value} - Debounced: {lastValue}
</p>
<button onClick={() => setValue(value + 1)}>Increment</button>
</div>
);
};
ReactDOM.createRoot(document.getElementById('root')).render(
<Counter />
);