Repository navigation
fix(createMemo): support changing argument counts - #2739
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 #2294.
createMemopasses the function's arguments directly touseMemoas its dependencies. React expects the dependency array to keep the same length, so changing the argument count while keeping the shared prefix can return a stale result. For example, a memoized sum called with[1, 2]and then[1, 2, 3]keeps returning3instead of6. Development builds also emit a dependency-array-size warning.Keep a stable reference to the arguments while their length and individual values remain equal using
Object.is, then use that reference as the singleuseMemodependency. This preserves the existing argument comparison semantics and per-hook-instance cache while supporting optional and rest parameters.Add ten regression and compatibility tests, including six argument-count transitions that fail before the fix. Document the comparison behavior and a variadic sum example.
Type of change
Checklist
Validation
Using Node 24.14.0, React 17.0.2, TypeScript 4.1.5 and the repository's existing dependency versions:
pnpm exec jest tests/createMemo.test.ts --runInBand --watchman=false --coverage --collectCoverageFrom=src/factory/createMemo.ts: 15 tests pass; 100% statements, branches, functions and lines.pnpm exec tsc --noEmit: passes.pnpm exec tscandpnpm exec tsc -m esNext --outDir esm: CJS and ESM builds pass.git diff --check: pass.3and verifies the corrected6, clearing arguments, and memoization across unchanged renders. The separate production-build browser run was interrupted when the browser task space became unavailable; it is not counted as validation.AI assistance was used for implementation, tests, verification and this PR description.