react-timing-hooks
react-timing-hooks copied to clipboard
fix: make useThrottle resilient to remounting
The cleanup function cleared the timeout without resetting the timeout ref, so in strict mode (or in general when React calls cleanup but then ends up not unmounting) the effect would get stuck, never actually calling the callback.
See: https://react.dev/learn/lifecycle-of-reactive-effects#how-react-verifies-that-your-effect-can-re-synchronize
I've encountered this bug while using Next.js in watch mode. Happy I could track it down to the root cause. The additional unit test fails without the proposed change.