Skip to content

Commit ae91e3a

Browse files
authored
[iOS] Fix buttons crash when unmounting children (#4065)
## Description `buttonView` is `UIControl`, which unlike `RCTViewComponentView` does strict checks for view index when unmounting children. Fixes #4062 ## Test plan <details> <summary>Tested on example from #4062</summary> ```tsx import { useState } from 'react'; import { Pressable, Text, View } from 'react-native'; import { Clickable, GestureHandlerRootView, } from 'react-native-gesture-handler'; export default function App() { const [children, setChildren] = useState<React.ReactNode[]>([]); return ( <GestureHandlerRootView style={[ { flex: 1, alignItems: 'center', justifyContent: 'center' }, { marginTop: 25 }, ]}> <Clickable> <Text>Clickable</Text> {children.map((child, index) => ( <View key={index}>{child}</View> ))} </Clickable> <Pressable onPress={() => setChildren([ ...children, <Text key={children.length}>New child</Text>, ]) }> <Text>Add child to Clickable</Text> </Pressable> <Pressable onPress={() => setChildren(children.slice(0, -1))}> <Text>Remove child from Clickable</Text> </Pressable> </GestureHandlerRootView> ); } ``` </details>
1 parent dac49b0 commit ae91e3a

1 file changed

Lines changed: 5 additions & 2 deletions

File tree

packages/react-native-gesture-handler/apple/RNGestureHandlerButtonComponentView.mm

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,12 @@ - (void)mountChildComponentView:(RNGHUIView<RCTComponentViewProtocol> *)childCom
7272
[_buttonView mountChildComponentView:childComponentView index:index];
7373
}
7474

75-
- (void)unmountChildComponentView:(RNGHUIView<RCTComponentViewProtocol> *)childComponentView index:(NSInteger)index
75+
- (void)unmountChildComponentView:(RNGHUIView<RCTComponentViewProtocol> *)childComponentView
76+
index:(NSInteger)__unused index
7677
{
77-
[_buttonView unmountChildComponentView:childComponentView index:index];
78+
if (childComponentView.superview == _buttonView) {
79+
[childComponentView removeFromSuperview];
80+
}
7881
}
7982

8083
- (LayoutMetrics)buildWrapperMetrics:(const LayoutMetrics &)metrics

0 commit comments

Comments
 (0)