Skip to content

fix: disconnect AnimatedProps before attaching a new native view - #59099

Merged
meta-codesync[bot] merged 2 commits into
react:mainfrom
kirillzyusko:fix/animated-node-already-attached
Nov 2, 2026
Merged

meta-codesync[bot] merged 2 commits into
react:mainfrom
kirillzyusko:fix/animated-node-already-attached

Conversation

@kirillzyusko

Copy link
Copy Markdown
Contributor

Summary:

When an animated component replaces its underlying native view, AnimatedProps.setNativeView() connects the existing animated props node to the replacement without disconnecting the previous view. On Android, this causes a crash:

Animated node X is already attached to a view: Y

Disconnect the previous target before connecting the replacement.

Changelog:

[ANDROID] [FIXED] - Prevent native Animated crashes when an animated component replaces its underlying native view.

Test Plan:

You can use this code to reproduce a crash:

import React, { forwardRef, useState } from "react";
import { Animated, Button, StyleSheet, Text, View } from "react-native";

import type { ViewProps } from "react-native";

// Keep the Animated wrapper mounted while replacing its native View.
const ReplacingView = Animated.createAnimatedComponent(
  forwardRef<View, ViewProps & { generation: number }>(function ReplacingView(
    { generation, ...props },
    ref,
  ) {
    return <View {...props} key={generation} ref={ref} />;
  }),
);

const styles = StyleSheet.create({
  screen: { flex: 1, padding: 24, paddingTop: 64, backgroundColor: "white" },
});

export default function Issue1604() {
  const [generation, setGeneration] = useState(0);
  const [opacity] = useState(
    () => new Animated.Value(1, { useNativeDriver: true }),
  );
  const replaceView = () => setGeneration((value) => value + 1);

  return (
    <View style={styles.screen}>
      <Text>Native Animated crash repro</Text>
      <Button title="Replace view" onPress={replaceView} />
      <ReplacingView generation={generation} style={{ opacity }}>
        <Text testID="issue1604-generation">View {generation}</Text>
      </ReplacingView>
    </View>
  );
}
Before After
before.mp4
after.mp4

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 15, 2026
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Oct 15, 2026
@kirillzyusko

Copy link
Copy Markdown
Contributor Author

Hey @cipolleschi 👋

Would it be possible to have a look on this PR? Or tag a person who knows Animated API and can review this PR? 🙏

@kirillzyusko

Copy link
Copy Markdown
Contributor Author

Hey @tomekzaw

Maybe you can review this PR? Or someone from SWM? I know that Animated is not part of your primary area (because you maintain reanimated), but anyway, it's related to animations 🤷‍♂️

Or maybe you know whom I should tag here to bring this PR to attention?..

@tomekzaw

Copy link
Copy Markdown
Contributor

Hey @kirillzyusko, I asked @bartlomiejbloniarz to take a look

@kirillzyusko

Copy link
Copy Markdown
Contributor Author

Thank you @tomekzaw ❤️

@meta-codesync

meta-codesync Bot commented Oct 29, 2026

Copy link
Copy Markdown

@bartlomiejbloniarz has imported this pull request. If you are a Meta employee, you can view this in D128344147.

@meta-codesync
meta-codesync Bot merged commit 856661f into react:main Nov 2, 2026
182 of 187 checks passed
@react-native-bot

Copy link
Copy Markdown
Collaborator

This pull request was successfully merged by @kirillzyusko in 856661f

When will my fix make it into a release? | How to file a pick request?

@react-native-bot react-native-bot added the Merged This PR has been merged. label Nov 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged This PR has been merged. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants