Skip to content

fix(hooks): avoid replaying change callbacks for handled actions - #1707

Open
Hanan88 wants to merge 1 commit into
downshift-js:masterfrom
Hanan88:codex/avoid-replayed-change-callbacks
Open

Hanan88 wants to merge 1 commit into
downshift-js:masterfrom
Hanan88:codex/avoid-replayed-change-callbacks

Conversation

@Hanan88

@Hanan88 Hanan88 commented Nov 21, 2026

Copy link
Copy Markdown

Pull Request

What

Fixes #1706. Notify change handlers once for a handled action, even when React reruns the shared reducer effect.

Why

With controlled values, the effect compares the controlled previous state with the raw reducer state. Re-running the effect for the same action can therefore repeat onSelectedItemChange and navigation or submission side effects when an Activity subtree is revealed. React 18 event rebasing can trigger the same mismatch.

How

Remember the last handled action in a ref and mark it before calling change handlers. Keep updating the previous-state ref as before. A new dispatch still notifies, including when the caller reuses its action object or changes callbacks.

Changes

  • Add the action guard to useEnhancedReducer.
  • Add a regression that reruns the effect with a controlled value, then verifies that a fresh dispatch uses the new callback.

Verification

  • Unit coverage: 92 suites, 1,112 tests and 49 snapshots pass; 100% statements, branches, functions and lines.
  • Build/package checks: 8 tests pass; web, Preact, React Native and native-web bundles build.
  • TypeScript check and SSR test pass.
  • Chromium against the built useSelect: React 18 same-frame move/click calls the handler once; React 19 Activity reveals with and without StrictMode do not replay it, while subsequent selections still notify.
  • The new unit regression and isolated Activity reproduction fail on the original reducer and pass with the guard.
  • Formatting passes. Normal lint hits an existing jest/unbound-method plugin crash (services.getSymbolAtLocation is not a function) on the unchanged mock at the top of the test file; confirmed using the upstream file through stdin. All other changed-file lint rules pass with that one rule omitted for the local check. No lint configuration or dependencies are changed in this PR.

Checklist

  • Documentation — N/A, no API change
  • Tests
  • TypeScript Types — N/A, no public type change
  • Ready to be merged — ready for review; see the baseline lint tooling issue above

AI assistance was used to prepare the fix and regression. The failure was reproduced before the change and verified locally against source and built APIs.

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.

useEnhancedReducer re-calls on*Change handlers when its effect re-runs for an already-handled action

1 participant