Repository navigation
fix: resume useThrottle after Strict Mode effect replay - #2743
Open
wanxiankai wants to merge 1 commit into
Open
wanxiankai wants to merge 1 commit into
wanxiankai wants to merge 1 commit into
Conversation
This branch has not been deployed
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.
Description
Fixes #2718.
React 18 Strict Mode replays effect setup and cleanup while preserving refs.
useThrottleclears its timer during cleanup but retains the cancelled timer ID, so the replayed effect treats it as active and subsequent values never publish.Reset the timeout ref and pending-value flag when cleaning up. Replayed effects can then start a new throttle interval, without publishing a pending value left over from the previous effect lifecycle. Normal leading and trailing updates keep their existing behavior.
Add five tests covering effect replay, repeated replay, pending-value disposal, idle cleanup, and successive throttle intervals. The project uses React 17 test dependencies, so the regression helper explicitly replays effect cleanup and setup while preserving hook refs. Three tests fail against the unchanged implementation.
Also add a Strict Mode Storybook example and document the supported lifecycle.
Type of change
Checklist
yarn test)yarn lint)yarn lint:types)Validation
yarn test: 76 suites, 497 tests pass.yarn jest tests/useThrottle.test.ts --runInBand --coverage --collectCoverageFrom=src/useThrottle.ts: 12 tests pass; 100% statements, branches, functions, and lines.yarn lint: passes with 0 errors and 97 existing warnings.yarn lint:types: passes.yarn build: CommonJS and ES module builds pass.git diff --check: pass.createRoot, and developmentStrictMode: confirms two effect setups and one replay cleanup. With identical input updates to 1 and then 4, the original hook stays at 0 while the fixed hook publishes 1 and 4. Final unmount invokes the second cleanup.The browser fixture uses separate dependencies and does not change the project's React version or lockfile.