1

On iOS, the app crashes when executing navigate("Screen1") after the execution of popToTop(). The app works perfectly fine on Android.

props.navigation.popToTop(); 
props.navigation.navigate("Screen1"); // This statement causes iOS to crash!

// This is related to a simple stack:
const SingleStack = () => {
    const MyStack = createNativeStackNavigator();
    const { theme } = useTheme(); 

    return (
    <MyStack.Navigator screenOptions={(params) => DefaultScreenOptions({ params, colors: theme.colors, sizes: theme.sizes, })} >
        <MyStack.Screen name="Screen1" component={Screen1} options={{ title: "Screen 1", }} />
        // ...
    </MyStack.Navigator>
    );
};

const DefaultScreenOptions = ({ params, colors, sizes, }) => {
    return ({
        headerTintColor: colors.headerTextColor,
        headerStyle: { backgroundColor: colors.headerBackgroundColor },
        headerRight: () => (
            <Icon name="bars" type="font-awesome"
                size={sizes.defaultIconSize * 1.5}
                color={colors.headerTextColor}
                onPress={() => params.navigation.dispatch(DrawerActions.toggleDrawer())}
                containerStyle={{ padding: 10, }} 
            />
        ),
    });
};

Bilal Abdeen
  • 1,627
  • 1
  • 19
  • 41

1 Answers1

0

As a workaround, I added a delay of (900 ms) before executing the navigation statement.

props.navigation.popToTop(); 
const timeout = setTimeout(() => {
    clearTimeout(timeout);
    props.navigation.navigate("Screen1");
}, (Platform.OS === "ios" ? 1500 : 100)); 

Edit on 14-Aug-2023, it failed with 900 ms. I had to increase the delay to 1500 ms!

Bilal Abdeen
  • 1,627
  • 1
  • 19
  • 41