iOS 应用程序在 popToTop() 之后导航时崩溃

问题描述 投票:0回答:1

在 iOS 上,在执行

navigate("Screen1")
后执行
popToTop()
时,应用程序会崩溃。该应用程序在 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, }} 
            />
        ),
    });
};

react-navigation react-navigation-stack react-navigation-v6
1个回答
0
投票

作为解决方法,我在执行导航语句之前添加了(900 毫秒)的延迟。

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

于 2023 年 8 月 14 日编辑,失败时间为 900 毫秒。我必须将延迟增加到 1500 毫秒!

© www.soinside.com 2019 - 2024. All rights reserved.