Skip to content

Commit 10bda7d

Browse files
alduzykkafar
authored andcommitted
fix(Android): incorrect childCount in removeViewAt when using flatlist on fabric (#2307)
This PR intents to fix the crash when navigating back from a screen with FlatList on the new architecture. The crash was caused by miscalculated `childCount` of the list. Earlier on I found out that setting the [removeClippedSubviews](https://reactnative.dev/docs/flatlist#removeclippedsubviews) option to false (defaults to true on Android) in the FlatList fixes the problem. This PR is rather a quick fix with an extra condition, that adds simple views in place of the miscalculated ones in `startTransitionRecursive` function if there's a FlatList with `removeClippedSubviews` option set. Fixes #2282. - added `Test2282.tsx` repro - added extra condition in `startTransitionRecursive` function <!-- Here you can add screenshots / GIFs documenting your change. You can add before / after section if you're changing some behavior. --> - added `Test2282.tsx` repro - [x] Ensured that CI passes (cherry picked from commit c47ad84)
1 parent a836acc commit 10bda7d

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

android/src/main/java/com/swmansion/rnscreens/Screen.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import com.facebook.react.bridge.ReactContext
1717
import com.facebook.react.uimanager.PixelUtil
1818
import com.facebook.react.uimanager.UIManagerHelper
1919
import com.facebook.react.uimanager.UIManagerModule
20+
import com.facebook.react.views.scroll.ReactScrollView
2021
import com.swmansion.rnscreens.events.HeaderHeightChangeEvent
2122

2223
@SuppressLint("ViewConstructor") // Only we construct this view, it is never inflated.
@@ -312,6 +313,15 @@ class Screen(
312313
startTransitionRecursive(child.toolbar)
313314
}
314315
if (child is ViewGroup) {
316+
// The children are miscounted when there's a FlatList with
317+
// removeCLippedSubviews set to true (default).
318+
// We add a simple view for each item in the list to make it work as expected.
319+
// See https://github.com/software-mansion/react-native-screens/issues/2282
320+
if (it is ReactScrollView && it.removeClippedSubviews) {
321+
for (j in 0 until child.childCount) {
322+
child.addView(View(context))
323+
}
324+
}
315325
startTransitionRecursive(child)
316326
}
317327
}

apps/src/tests/Test2282.tsx

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import React from 'react';
2+
import { FlatList, Button, Text } from 'react-native';
3+
import { createNativeStackNavigator } from '@react-navigation/native-stack';
4+
import { NavigationContainer } from '@react-navigation/native';
5+
6+
const Stack = createNativeStackNavigator();
7+
8+
const First = ({ navigation }: any) => (
9+
<Button onPress={() => navigation.navigate('Second')} title="Navigate" />
10+
);
11+
12+
const Second = ({ navigation }: any) => (
13+
<>
14+
<FlatList
15+
renderItem={({ item }) => <Text>{item}</Text>}
16+
data={[1,2,3,4,5,6]}
17+
keyExtractor={item => item.toString()}
18+
/>
19+
<Button onPress={navigation.goBack} title="Go back" />
20+
</>
21+
);
22+
23+
export default function App() {
24+
return (
25+
<NavigationContainer>
26+
<Stack.Navigator>
27+
<Stack.Screen name="First" component={First} />
28+
<Stack.Screen name="Second" component={Second} />
29+
</Stack.Navigator>
30+
</NavigationContainer>
31+
);
32+
}

apps/src/tests/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,5 +109,6 @@ export { default as Test2232 } from './Test2232';
109109
export { default as Test2235 } from './Test2235';
110110
export { default as Test2252 } from './Test2252';
111111
export { default as Test2271 } from './Test2271';
112+
export { default as Test2282 } from './Test2282';
112113
export { default as TestScreenAnimation } from './TestScreenAnimation';
113114
export { default as TestHeader } from './TestHeader';

0 commit comments

Comments
 (0)