Skip to content

fix: call composite handlers from the latest render in fireEvent - #1965

Merged
mdjastrzebski merged 12 commits into
callstack:mainfrom
lassiecoder:fix/stale-composite-handler
Dec 1, 2026
Merged

mdjastrzebski merged 12 commits into
callstack:mainfrom
lassiecoder:fix/stale-composite-handler

Conversation

@lassiecoder

@lassiecoder lassiecoder commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #1961.

fireEvent looks for handlers on composite components by walking up from the host element's unstable_fiber through return pointers and reading memoizedProps. React keeps two copies of each fiber (fiber and fiber.alternate) and switches between them on every commit. When React skips the host element on a re-render, for example because the composite returns a memoized element or renders a React.memo child, test-renderer doesn't call commitUpdate, and the walk can reach the copy from an earlier commit. Its props hold the handlers of an earlier render, so every other event calls a stale handler.

This adds getCurrentFiber() (src/helpers/fiber.ts), based on React's findCurrentFiberUsingSlowPath, which react-reconciler doesn't export. findEventHandlerFromFiber resolves the current copy at each step before reading its props. Resolving only the starting fiber is not enough: in both test cases below the stale copy is reached through return pointers, and the results are the same as without the fix. When the current copy can't be determined (e.g. an unmounted fiber), it keeps the fiber it was given, as before.

Other options: the modern event system in #1945 doesn't read composite props this way, but it is opt-in, and the legacy path is the default in 14.x. The fix could also live in test-renderer, so unstable_fiber always points at the current fiber; that would not cover the return pointers, which are React's.

Test plan

  • src/events/__tests__/stale-handler.test.tsx: a composite gets a new onPress on every render and either returns the same host element (useMemo) or renders a React.memo child. Four presses, each re-rendering the parent, must call the handlers from renders 1, 2, 3, 4. On main both cases get [1, 1, 3, 3].
  • yarn validate (typecheck, 88 suites / 956 tests, lint, format:check) and yarn typecheck:react-19_2 pass.
  • Tested with the versions in this repo (React 19.3.0, test-renderer 1.3.1, react-native 0.88.0-rc.1). The reporter also confirmed it in their app on test-renderer 1.2.0, React 19.2.3 and react-native 0.87.1, with all ~4k tests passing.

@mlisikbf

Copy link
Copy Markdown

Thx @lassiecoder for putting this together! Can confirm this fixes the issue on our end (both test project and the product repo) + no regression, all 4k tests pass. That ran with test-renderer 1.2.0 / React 19.2.3 / React Native 0.87.1.

@lassiecoder

Copy link
Copy Markdown
Contributor Author

Thanks for testing it on your app, that's really helpful, especially on other versions (test-renderer 1.2.0, React 19.2.3, RN 0.87.1) than the repo's.

mdjastrzebski and others added 2 commits November 30, 2026 09:31
…wPath

Check `a` before `b` in the bailout branch, as React does, and replace
comments with references to the React implementation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mdjastrzebski

Copy link
Copy Markdown
Member

Seems like walking the fiber tree is more complex then the original code had. Thanks for submitting this, I plan to merge & release this soon

@codecov

codecov Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.60%. Comparing base (32b3d1e) to head (9988095).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1965      +/-   ##
==========================================
+ Coverage   98.57%   98.60%   +0.02%     
==========================================
  Files          87       88       +1     
  Lines        1750     1786      +36     
  Branches      460      492      +32     
==========================================
+ Hits         1725     1761      +36     
  Misses         24       24              
  Partials        1        1              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

mdjastrzebski and others added 9 commits November 30, 2026 09:57
Check every committed fiber and its alternate against the tree reachable
from the root's `current`, shorten the stale handler test, and ignore
branches React itself treats as unreachable.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Corrected issue: keep fixtures and helpers above the tests (house style
in src/ test files), but order the helper chain callers-first, as
contributing/code-style.md requires.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…under test

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Keep the knowledge that a HostRoot's `stateNode` is React's FiberRoot in
one place, shared by getCurrentFiber and its tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Move each reason to its own comment line above a bare istanbul hint, as in
timers.ts and wait-for.ts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@lassiecoder

Copy link
Copy Markdown
Contributor Author

Seems like walking the fiber tree is more complex then the original code had. Thanks for submitting this, I plan to merge & release this soon

Thanks, @mdjastrzebski! Happy to adjust anything before the release. :)

@mdjastrzebski
mdjastrzebski merged commit 3e5564e into callstack:main Dec 1, 2026
35 checks passed
@mdjastrzebski

Copy link
Copy Markdown
Member

Released in v14.1.1 🎉

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.

fireEvent calls a composite's handler from an earlier render when the host element's props don't change

3 participants