Skip to content

fix(createMemo): support changing argument counts - #2739

Open
wanxiankai wants to merge 1 commit into
streamich:masterfrom
wanxiankai:fix/2294-variadic-memo-arguments
Open

wanxiankai wants to merge 1 commit into
streamich:masterfrom
wanxiankai:fix/2294-variadic-memo-arguments

Conversation

@wanxiankai

Copy link
Copy Markdown

Description

Fixes #2294.

createMemo passes the function's arguments directly to useMemo as 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 returning 3 instead of 6. 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 single useMemo dependency. 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

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as before)

Checklist

  • Read the Contributing Guide
  • Perform a code self-review
  • Review the need for code comments; the change has no complex section requiring an inline comment
  • Add documentation
  • Review Storybook coverage; not applicable to this existing factory's argument-comparison fix, which is covered by tests and a documentation example
  • Cover changes with tests
  • Ensure the test suite passes
  • Provide 100% tests coverage for the changed factory
  • Make sure code lints
  • Make sure types are fine

Validation

Using Node 24.14.0, React 17.0.2, TypeScript 4.1.5 and the repository's existing dependency versions:

  • Before the source fix: six argument-count regressions fail; nine focused tests pass.
  • 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.
  • Full Jest suite with Watchman disabled: 76 suites / 502 tests pass.
  • pnpm exec tsc --noEmit: passes.
  • pnpm exec tsc and pnpm exec tsc -m esNext --outDir esm: CJS and ESM builds pass.
  • Full source/test/story ESLint: zero errors and 95 existing warnings; changed files lint without warnings.
  • Prettier check using the options from the repository's ESLint configuration and git diff --check: pass.
  • An Ego Lite browser comparison using the actual original and patched source with React's development build reproduces the stale 3 and verifies the corrected 6, 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.

This branch has not been deployed

No deployments
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.

createMemo does not support variadic functions

1 participant