Skip to content

fix(Reanimated): move a forwarded ref when it changes after mount - #11775

Open
Neelesh-FS-Dev wants to merge 1 commit into
software-mansion:mainfrom
Neelesh-FS-Dev:fix/animated-component-forwarded-ref
Open

Neelesh-FS-Dev wants to merge 1 commit into
software-mansion:mainfrom
Neelesh-FS-Dev:fix/animated-component-forwarded-ref

Conversation

@Neelesh-FS-Dev

Copy link
Copy Markdown

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

Fixes #10869

Test plan

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.

🤖 Generated with Claude Code

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.

Fixes software-mansion#10869
@coderabbitai

coderabbitai Bot commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Walkthrough

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.

Possibly related PRs

Suggested labels: lib: Reanimated

Change: Bug fix

Merge Risk: Moderate · up to 97557

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: Passed 3 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Linked Issues check Warning 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 Passed checks: 3 passed

Check name Status Explanation
Title check Passed 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 Passed 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 Passed 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.

Share

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments

Major 2
  1. Changed forwarded refs must detach before any replacement attaches.
  2. CSS animated components do not sync a changed forwardedRef.
Minor 1
  1. Invalidate the raw ref when the child detaches.
Review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: dab3de45-40ca-4335-996b-9e9bd8ce6574
Commits

Reviewing files that changed from the base of the PR and between 1cd0ba2 and 97557d1.

Files selected for processing: 6

  • packages/react-native-reanimated/__tests__/animatedComponentRefChange.test.tsx
  • packages/react-native-reanimated/changelog/animated-component-forwarded-ref.fix.md
  • packages/react-native-reanimated/src/createAnimatedComponent/AnimatedComponent.native.tsx
  • packages/react-native-reanimated/src/createAnimatedComponent/AnimatedComponent.tsx
  • packages/react-native-reanimated/src/css/component/AnimatedComponent.native.tsx
  • packages/react-native-reanimated/src/css/component/AnimatedComponent.tsx

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.

this._onSetLocalRef();
};

_syncForwardedRef() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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.

Comment on lines +143 to +147
assignRef(this._attachedForwardedRef as Ref<unknown>, null);
}
this._attachedForwardedRef = forwardedRef;
this._forwardedRefCleanup = this._rawComponentRef
? assignRef(forwardedRef as Ref<unknown>, this._rawComponentRef)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Comment on lines +144 to +145
this._forwardedRefCleanup = this._rawComponentRef
? assignRef(forwardedRef as Ref<unknown>, this._rawComponentRef)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Animated components ignore a ref that changes after mount

1 participant