Repository navigation
fix: call composite handlers from the latest render in fireEvent - #1965
lassiecoder wants to merge 3 commits into
Conversation
|
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. |
|
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. |
…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>
…andler # Conflicts: # CHANGELOG.md
|
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 Report❌ Patch coverage is
❌ 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. 🚀 New features to boost your workflow:
|
Summary
Fixes #1961.
fireEventlooks for handlers on composite components by walking up from the host element'sunstable_fiberthroughreturnpointers and readingmemoizedProps. React keeps two copies of each fiber (fiberandfiber.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 aReact.memochild,test-rendererdoesn't callcommitUpdate, 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'sfindCurrentFiberUsingSlowPath, whichreact-reconcilerdoesn't export.findEventHandlerFromFiberresolves 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 throughreturnpointers, 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, sounstable_fiberalways points at the current fiber; that would not cover thereturnpointers, which are React's.Test plan
src/events/__tests__/stale-handler.test.tsx: a composite gets a newonPresson every render and either returns the same host element (useMemo) or renders aReact.memochild. Four presses, each re-rendering the parent, must call the handlers from renders 1, 2, 3, 4. Onmainboth cases get[1, 1, 3, 3].yarn validate(typecheck, 88 suites / 956 tests, lint, format:check) andyarn typecheck:react-19_2pass.