When calling just the debounce function like this:
const handler = debounce(someFunction, 2000);
It will call the someFunction
on every keystroke. But when we wrap it in useCallback, it works fine.
const handler = useCallback(debounce(someFunction, 2000), []);
But as far as I know debounce function should call the someFunction
after 2000 ms and so the function should not be called on every keystroke. But it is not what I expected.
Can anyone explain why useCallback needed when using debounce?
When making the switch from off to on, the signal “bounces”. So “debouncing” is just cleaning up this noise. The system debounces this signal by only reacting to the new state when the signal has stayed the same for a certain minimum amount of time.
useCallback will return a memoized version of the callback that only changes if one of the dependencies has changed. This is useful when passing callbacks to optimized child components that rely on reference equality to prevent unnecessary renders (e.g. shouldComponentUpdate ).
The React useCallback Hook returns a memoized callback function. Think of memoization as caching a value so that it does not need to be recalculated. This allows us to isolate resource intensive functions so that they will not automatically run on every render.
It depends in which scope the handler
defined.
If handler
defined in the outer scope of the component, it will work exactly the same:
// Defined only once on file read
const handler = debounce(someFunction, 2000);
const Component = () => {
return <button onClick={handler}>Run</button>;
};
But, if it is defined in the inner scope, it actually redefines the function on every render, hence you reset the debounce on every render.
That's why on every key stroke, no debounce is applied (it resets on every render, which gives the feeling that it does not work).
// Defined on every render
const Component = () => {
const handler = debounce(someFunction, 2000);
return <button onClick={handler}>Run</button>;
};
Wrapping the function with useCallback
fixes exactly that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With