如何在React Native的TabNavigator中切换到其他选项卡并重置所有选项卡

时间:2019-05-07 06:00:20

标签: react-native react-navigation react-native-navigation

以多种不同的形式询问了此问题,我整周都进行了搜索,但无法确定如何在下面的设置中从Connect回到Devices

const HomeStack = createStackNavigator({
    Devices: DevicesScreen,
    Details: DetailsScreen, // go here
})

const NewDeviceStack = createStackNavigator({
    Start: StartScreen,
    Connect: ConnectScreen, // start here
})

const BottomTabs = createBottomTabNavigator(
{
    Home: { screen: HomeStack },
    Settings: { screen: NewDeviceStack },
}

export default createAppContainer(BottomTabs);

Connect屏幕上,此代码尝试重置当前堆栈并返回到Devices

const resetAction = StackActions.reset({
        index: 0,
        key: null,
        actions: [
            NavigationActions.navigate({routeName: 'Devices'})
            ],
        })
this.props.navigation.dispatch(resetAction)

此操作失败,并显示以下信息:

Error: There is no route defined for key Devices.
Must be one of: 'Start','Connect'

1 个答案:

答案 0 :(得分:1)

使用navigation道具,单击按钮即可导航到设备屏幕

this.props.navigation.navigate('Devices')

或仅创建一个处理导航的功能

goToScreen = (routeName, params = {}) => {

  const navigateAction = NavigationActions.navigate({
    routeName: routeName,

    params: params, // parameter

    action: NavigationActions.navigate({ routeName: routeName }), // screen you want to navigate to
  });

  this.props.navigation.dispatch(navigateAction);

};