Repository navigation
Commit 856661f
authored
fix: disconnect
## 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:
```text
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:
```tsx
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|
|-------|-----|
|<video src="https://github.com/user-attachments/assets/40f5b75f-e250-4c06-b00b-7084a2011064">|<video src="https://github.com/user-attachments/assets/a2524013-3923-4b8f-ac01-d0ede0f8edc2">|
Differential Revision: D128344147
Pull Request resolved: #59099AnimatedProps before attaching a new native view1 parent a9790c7 commit 856661f
2 files changed
Lines changed: 96 additions & 0 deletions
File tree
- packages/react-native
- Libraries/Animated/nodes
- src/private/animated/__tests__
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
268 | 268 | | |
269 | 269 | | |
270 | 270 | | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
271 | 274 | | |
272 | 275 | | |
273 | 276 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
34 | 39 | | |
35 | 40 | | |
36 | 41 | | |
| |||
1620 | 1625 | | |
1621 | 1626 | | |
1622 | 1627 | | |
| 1628 | + | |
| 1629 | + | |
| 1630 | + | |
| 1631 | + | |
| 1632 | + | |
| 1633 | + | |
| 1634 | + | |
| 1635 | + | |
| 1636 | + | |
| 1637 | + | |
| 1638 | + | |
| 1639 | + | |
| 1640 | + | |
| 1641 | + | |
| 1642 | + | |
| 1643 | + | |
| 1644 | + | |
| 1645 | + | |
| 1646 | + | |
| 1647 | + | |
| 1648 | + | |
| 1649 | + | |
| 1650 | + | |
| 1651 | + | |
| 1652 | + | |
| 1653 | + | |
| 1654 | + | |
| 1655 | + | |
| 1656 | + | |
| 1657 | + | |
| 1658 | + | |
| 1659 | + | |
| 1660 | + | |
| 1661 | + | |
| 1662 | + | |
| 1663 | + | |
| 1664 | + | |
| 1665 | + | |
| 1666 | + | |
| 1667 | + | |
| 1668 | + | |
| 1669 | + | |
| 1670 | + | |
| 1671 | + | |
| 1672 | + | |
| 1673 | + | |
| 1674 | + | |
| 1675 | + | |
| 1676 | + | |
| 1677 | + | |
| 1678 | + | |
| 1679 | + | |
| 1680 | + | |
| 1681 | + | |
| 1682 | + | |
| 1683 | + | |
| 1684 | + | |
| 1685 | + | |
| 1686 | + | |
| 1687 | + | |
| 1688 | + | |
| 1689 | + | |
| 1690 | + | |
| 1691 | + | |
| 1692 | + | |
| 1693 | + | |
| 1694 | + | |
| 1695 | + | |
| 1696 | + | |
| 1697 | + | |
| 1698 | + | |
| 1699 | + | |
| 1700 | + | |
| 1701 | + | |
| 1702 | + | |
| 1703 | + | |
| 1704 | + | |
| 1705 | + | |
| 1706 | + | |
| 1707 | + | |
| 1708 | + | |
| 1709 | + | |
| 1710 | + | |
| 1711 | + | |
| 1712 | + | |
| 1713 | + | |
| 1714 | + | |
| 1715 | + | |
1623 | 1716 | | |
1624 | 1717 | | |
1625 | 1718 | | |
| |||
0 commit comments