fix: make useThrottle resilient to remounting#42
Open
deltaidea wants to merge 1 commit intoEricLambrecht:mainfrom
Open
fix: make useThrottle resilient to remounting#42deltaidea wants to merge 1 commit intoEricLambrecht:mainfrom
deltaidea wants to merge 1 commit intoEricLambrecht:mainfrom
Conversation
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
EricLambrecht
requested changes
Jan 8, 2026
Owner
There was a problem hiding this comment.
Thank you for filing this pull request! I very much appreciate it. I have one suggested change, to have more coverage. My suggestions seem to work with your fix. Also, please rebase on main. Thanks! :)
Edit: I just noticed, that useDebounce will probably face the same issue. If you want to, you can fix it, too!
| jest.runAllTimers() | ||
| }) | ||
|
|
||
| expect(timeoutHandler).toHaveBeenCalledTimes(1) |
Owner
There was a problem hiding this comment.
Suggested change
| expect(timeoutHandler).toHaveBeenCalledTimes(1) | |
| expect(timeoutHandler).toHaveBeenCalledTimes(1) | |
| expect(clearTimeoutSpy).toHaveBeenCalledTimes(1) |
Owner
There was a problem hiding this comment.
I suggest that we test both the execution and cleanup call in both new and old tests.
| @@ -232,6 +232,26 @@ describe('useThrottle', () => { | |||
| expect(clearTimeoutSpy).toHaveBeenCalledTimes(1) | |||
Owner
There was a problem hiding this comment.
Suggested change
| expect(clearTimeoutSpy).toHaveBeenCalledTimes(1) | |
| expect(timeoutHandler).toHaveBeenCalledTimes(1) | |
| expect(clearTimeoutSpy).toHaveBeenCalledTimes(1) |
See my other comment at L252
| "@shopify/jest-dom-mocks": "^5.2.0", | ||
| "@testing-library/jest-dom": "^6.5.0", | ||
| "@testing-library/react": "^16.0.1", | ||
| "@testing-library/react": "^16.3.1", |
Owner
There was a problem hiding this comment.
I recently updated the testing library packages, so this is now obsolete.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.