Skip to content

fix: make useThrottle resilient to remounting#42

Open
deltaidea wants to merge 1 commit intoEricLambrecht:mainfrom
deltaidea:fix-use-throttle-cleanup
Open

fix: make useThrottle resilient to remounting#42
deltaidea wants to merge 1 commit intoEricLambrecht:mainfrom
deltaidea:fix-use-throttle-cleanup

Conversation

@deltaidea
Copy link

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.

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
Copy link
Owner

@EricLambrecht EricLambrecht left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
expect(timeoutHandler).toHaveBeenCalledTimes(1)
expect(timeoutHandler).toHaveBeenCalledTimes(1)
expect(clearTimeoutSpy).toHaveBeenCalledTimes(1)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recently updated the testing library packages, so this is now obsolete.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants