function Foo() {
const [state, setState] = useState(0);
const cb = useCallback(debounce(() => {
console.log(state);
}, 1000), []);
return ...;
}
In this example, state
can become stale in the callback. One way I can think of to fix this is something like:
function Foo() {
const [state, setState] = useState(0);
const cbHelper = useCallback(debounce((state2) => {
console.log(state2);
}, 1000), [])
const cb = () => cbHelper(state);
return ...;
}
However, this looks pretty messy. Is there a cleaner/better way to do this?
Edit:
I can't just do the following because debounce
won't work:
useCallback(debounce(() => {
console.log(state);
}, 1000), [state]);