在React-Navigation V5/V6的屏幕外获取Navigation.State

发布于 2025-02-04 20:39:33 字数 1385 浏览 4 评论 0原文

我正在尝试将一个反应的V4应用程序升级到V5/V6。在v4中,createAppContainer被像这样:

const AppContainer = createAppContainer(AppNavigator);

appnavigator将接收navigation prop,可以使用:

type AppNavigatorProps = {
    navigation: Navigation;
};

class AppNavigator extends Component<AppNavigatorProps> {

    isSplashScreen = (): boolean => {
        const { navigation } = this.props;
        return navigation.state.index === RouteIndexes.SplashScreen;
    };
    render(): ReactNode {
        // styling set-up here

        return (
            <>
                <SafeAreaView style={topStyle} />
                <SafeAreaView style={bottomStyle}>
                    <StatusBar backgroundColor={barColor} />
                    <ErrorBoundary
                        navigation={navigation}>
                        <RootContainerStackNavigator navigation={navigation} />
                    </ErrorBoundary>
                </SafeAreaView>
            </>
        );
    }
}

我无法找到一种方法再在此位置中获取该navigation.state。我有appnavigator navigationContainer而不是createAppContainer so:

<NavigationContainer>
    <AppNavigator />
</NavigationContainer>

I'm trying to upgrade a react-native v4 app to v5/v6. In v4, createAppContainer was used like so:

const AppContainer = createAppContainer(AppNavigator);

and AppNavigator would receive a Navigation prop that could be used like:

type AppNavigatorProps = {
    navigation: Navigation;
};

class AppNavigator extends Component<AppNavigatorProps> {

    isSplashScreen = (): boolean => {
        const { navigation } = this.props;
        return navigation.state.index === RouteIndexes.SplashScreen;
    };
    render(): ReactNode {
        // styling set-up here

        return (
            <>
                <SafeAreaView style={topStyle} />
                <SafeAreaView style={bottomStyle}>
                    <StatusBar backgroundColor={barColor} />
                    <ErrorBoundary
                        navigation={navigation}>
                        <RootContainerStackNavigator navigation={navigation} />
                    </ErrorBoundary>
                </SafeAreaView>
            </>
        );
    }
}

I cannot figure out a way to get that navigation.state in this location anymore. I have the AppNavigator wrapped in NavigationContainer instead of the createAppContainer like so:

<NavigationContainer>
    <AppNavigator />
</NavigationContainer>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文