首页 > 解决方案 > 导航堆栈为空时如何导航到特定屏幕?

问题描述

我怎样才能做到这一点:

if navigation stack is not empty
    this.props.navigation.goBack()
else
    this.props.navigation.navigate('CustomScreen')

标签: react-nativereact-navigation

解决方案


解决方法是从前一个屏幕发送参数,并在当前屏幕检查该参数是否存在

屏幕1:

this.props.navigation.navigate('Screen2', {prevScreen: 'Screen1'});

屏幕2:

if(this.props.navigation.getParam('prevScreen', null))
    this.props.navigation.goBack();
else
    this.props.navigation.navigate('CustomScreen');

推荐阅读