Skip to content

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

Open
lassiecoder wants to merge 3 commits into
callstack:mainfrom
lassiecoder:fix/stale-composite-handler
Open

lassiecoder wants to merge 3 commits into
callstack:mainfrom
lassiecoder:fix/stale-composite-handler

Conversation

@lassiecoder

@lassiecoder lassiecoder commented Nov 13, 2026 •

Copy link
Copy Markdown

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
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

❌ Patch coverage is 50.00000% with 26 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.16%. Comparing base (32b3d1e) to head (d791e39).

Files with missing lines Patch % Lines
src/helpers/fiber.ts 42.22% 26 Missing ⚠️

❌ Your patch check has failed because the patch coverage (50.00%) is below the target coverage (80.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1965      +/-   ##
==========================================
- Coverage   98.57%   97.16%   -1.41%     
==========================================
  Files          87       88       +1     
  Lines        1750     1798      +48     
  Branches      460      495      +35     
==========================================
+ Hits         1725     1747      +22     
- Misses         24       50      +26     
  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.

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.

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

3 participants