Repository navigation
fix: call composite handlers from the latest render in fireEvent - #1965
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✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
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>
Thanks, @mdjastrzebski! Happy to adjust anything before the release. :) |
|
Released in v14.1.1 🎉 |
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.