You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(Reanimated): move a forwarded ref when it changes after mount - #11775
This pull request was authored by AI on behalf of Neelesh-FS-Dev.
Summary
React calls a child's ref callback only when the child's ref changes. An animated component passes its forwarded ref through a stable callback, so when only the forwarded ref changed, the old ref kept the instance and the new ref stayed null. A plain View does this correctly.
This PR records the attached forwarded ref and, in componentDidUpdate, detaches the old one and attaches the new one. The change is applied to both the web and native variants of css/component/AnimatedComponent, because Jest loads the web variant.
Behaviour note: an inline callback ref is re-attached on each render, which matches what React does for a plain View.
New tests in packages/react-native-reanimated/__tests__/animatedComponentRefChange.test.tsx. The two Animated.View cases failed before this change and pass after it:
cd packages/react-native-reanimated
yarn jest __tests__/animatedComponentRefChange.test.tsx
Expected: after a rerender with a new ref, the old ref is null and the new ref is the view. A ref added after mount is also set. The full Reanimated Jest suite passes (159 suites).
Changelog
I added a changelog fragment with yarn changelog:add for each changed package, or this PR does not change react-native-reanimated or react-native-worklets.
React calls the child's ref callback only when the child's ref changes,
so a ref that changed on an Animated component was never moved. Sync the
forwarded ref in componentDidUpdate, as View does.
Fixessoftware-mansion#10869
Animated components now synchronize forwarded refs when the ref prop changes after mount. The update logic clears or cleans up the previous ref and assigns the current raw component ref to its replacement. Tests cover ref replacement and refs added after mount on View and Animated.View.
Ref updates now work for Animated.View, but CSS-only animated components still ignore a changed ref. Swapping refs between sibling components can leave one ref null, and a replacement ref can receive an unmounted host in edge cases. These gaps should be fixed before merging.
Pre-merge checks: 3 · 1
Failed checks: 1 warning
Check name
Status
Explanation
Resolution
Linked Issues check
Issue #10869 requires animated components made with createAnimatedComponent, including Animated.View, to detach an old ref and attach a new or added ref after rerender. The PR implements this only in …
Either define _syncForwardedRef and _attachedForwardedRef in src/createAnimatedComponent/AnimatedComponent.tsx and its native variant with behavior matching the css variant, or remove the added this._syncForwardedRef() call from both files.…
Passed checks: 3 passed
Check name
Status
Explanation
Title check
The title names the change: a forwarded ref that changes after mount is moved to the new ref. The changeset adds _syncForwardedRef() to the AnimatedComponent web and native variants, called from compo…
Description check
The description states the cause (React calls a ref callback only when the child's ref changes, and a stable callback wrapper leaves the new forwarded ref null), the fix (record the attached forwarded…
Out of Scope Changes check
All changed files relate to issue #10869. The css AnimatedComponent web and native files add the forwarded-ref sync logic. The createAnimatedComponent files add a call to that logic. The new test file…
Full details: Title check
Explanation
The title names the change: a forwarded ref that changes after mount is moved to the new ref. The changeset adds _syncForwardedRef() to the AnimatedComponent web and native variants, called from componentDidUpdate, and adds tests for ref replacement and ref addition after mount. The title is specific to the main fix and matches the changelog fragment.
Full details: Description check
Explanation
The description states the cause (React calls a ref callback only when the child's ref changes, and a stable callback wrapper leaves the new forwarded ref null), the fix (record the attached forwarded ref and detach/attach it in componentDidUpdate for web and native variants), the inline-callback behaviour, the test file, and the linked issue. It relates directly to the changeset.
Full details: Out of Scope Changes check
Explanation
All changed files relate to issue #10869. The css AnimatedComponent web and native files add the forwarded-ref sync logic. The createAnimatedComponent files add a call to that logic. The new test file covers View and Animated.View ref changes. The changelog fragment describes the same fix. No unrelated change is shown in the PR summary or the diff inspected. The defect in the createAnimatedComponent call is a correctness problem within the linked scope and is reported under the linked-issue assessment.
Full details: Linked Issues check
Explanation
Issue #10869 requires animated components made with createAnimatedComponent, including Animated.View, to detach an old ref and attach a new or added ref after rerender. The PR implements this only in src/css/component/AnimatedComponent.tsx and AnimatedComponent.native.tsx, which add _attachedForwardedRef and _syncForwardedRef(). The PR also adds one call, this._syncForwardedRef(), to componentDidUpdate in src/createAnimatedComponent/AnimatedComponent.tsx (and the matching native file, per the change summary). The diff for src/createAnimatedComponent/AnimatedComponent.tsx adds no _syncForwardedRef definition, and the base file does not define one. Its componentDidUpdate therefore calls an undefined method. The legacy createAnimatedComponent path gets no ref fix. The added call can throw a TypeError on updates. The two Animated.View test cases cover the css variant that Jest loads. They do not cover the createAnimatedComponent path. Coding requirement for the legacy path is unmet, and the change adds a possible runtime error there.
Resolution
Either define _syncForwardedRef and _attachedForwardedRef in src/createAnimatedComponent/AnimatedComponent.tsx and its native variant with behavior matching the css variant, or remove the added this._syncForwardedRef() call from both files. Add a Jest test that exercises a createAnimatedComponent-based component (not only the css Animated.View) for ref replacement and ref added after mount.
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.
The reason will be displayed to describe this comment to others. Learn more.
Major · Functional Correctness · Quick win
CSS animated components do not sync a changed forwardedRef.
Components made by createCSSAnimatedComponent never call _syncForwardedRef after mount. When the ref changes from A to B, the component keeps A attached, and B is never assigned. The same gap exists in the web base.
Add componentDidUpdate() to the CSS native and web AnimatedComponent bases. Call this._syncForwardedRef() from it. The regular Animated components already call the helper from their own componentDidUpdate. They extend ReanimatedAnimatedComponent, not the CSS base, so the new hook does not assign their refs a second time.
The reason will be displayed to describe this comment to others. Learn more.
Major · Functional Correctness · Heavy lift
Changed forwarded refs must detach before any replacement attaches.
When two mounted siblings swap object refs, one ref can end as null. For example, the left child owns ref A and the right child owns ref B, and each child receives the other ref. The left child's sync clears A and assigns B to itself. The right child's sync then clears B, which removes the left child's assignment, and assigns A to itself. B ends as null. If the update order reverses, A ends as null instead.
Plain View does not have this problem. React detaches affected refs before it attaches new values in the same commit. Apply the same ordering to the web AnimatedComponent, which this PR also changes. Detach every changed forwarded ref across the commit before any replacement ref is attached.
Ordering reference
React applies this order to ordinary refs. It sets affected ref.current values to null before updating the DOM (react.dev).
The reason will be displayed to describe this comment to others. Learn more.
Minor · Functional Correctness · Quick win
Invalidate the raw ref when the child detaches.
If a ref-forwarding child changes from rendering H to rendering null, _setComponentRef(null) leaves _rawComponentRef equal to H. If the same update replaces forwarded ref A with B, this branch assigns removed host H to B.
Clear _rawComponentRef on detachment in both platform implementations. A later non-null attachment can restore it. Keep the current assignment when the child remains mounted.
This branch has not been deployed
No deployments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Note
This pull request was authored by AI on behalf of Neelesh-FS-Dev.
Summary
React calls a child's ref callback only when the child's ref changes. An animated component passes its forwarded ref through a stable callback, so when only the forwarded ref changed, the old ref kept the instance and the new ref stayed
null. A plainViewdoes this correctly.This PR records the attached forwarded ref and, in
componentDidUpdate, detaches the old one and attaches the new one. The change is applied to both the web and native variants ofcss/component/AnimatedComponent, because Jest loads the web variant.Behaviour note: an inline callback ref is re-attached on each render, which matches what React does for a plain
View.Fixes #10869
Test plan
New tests in
packages/react-native-reanimated/__tests__/animatedComponentRefChange.test.tsx. The twoAnimated.Viewcases failed before this change and pass after it:cd packages/react-native-reanimated yarn jest __tests__/animatedComponentRefChange.test.tsxExpected: after a rerender with a new ref, the old ref is
nulland the new ref is the view. A ref added after mount is also set. The full Reanimated Jest suite passes (159 suites).Changelog
yarn changelog:addfor each changed package, or this PR does not changereact-native-reanimatedorreact-native-worklets.🤖 Generated with Claude Code