Skip to content

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

Open
kirillzyusko wants to merge 1 commit into
react:mainfrom
kirillzyusko:fix/animated-node-already-attached
Open

kirillzyusko wants to merge 1 commit 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

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

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. 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.

1 participant