DEV Community

Discussion on: Create a debounce function from scratch in typescript

Collapse
 
codingopinions profile image
David

Yeah, great contribution, but I was doing some tests and or I am doing something wrong or we need to enclose the call to the debounce function in the custom hook in a useMemo to keep the same instance between renders.

const [debouncedFun, teardown] = useMemo(
() => debounce(fn, ms),
[fn, ms],
);

Collapse
 
bwca profile image
Volodymyr Yepishev

Excellent point, David! :D