React Navigation:如何使用子堆栈中的特定选项卡返回到根选项卡导航器

时间:2018-06-09 12:40:47

标签: javascript reactjs react-native react-navigation

我在React Native中使用 react-navigation v2 ,并且只要想要返回到根导航器中的特定标签,就会卡住。

我有以下路线堆栈:

const HomeStack = createStackNavigator(
  {
    Home: Home,
    CreateInvoice: CreateInvoiceScreen,
    InvoiceSummary: InvoiceSummaryScreen,
    PinEntry: PinEntryScreen
  },
  {
    navigationOptions: {
      header: null
    }
  }
);

const CustomersStack = createStackNavigator(
  {
    Customers: CustomersScreen,
    Details: CustomerDetailsScreen
  },
  {
    navigationOptions: {
      header: null
    }
  }
);

const Tab = createBottomTabNavigator(
  {
    Home: HomeStack,
    Transactions: TransactionsTab,
    Customers: CustomersStack,
    Settings: SettingsTab
  }
);

const Routers = createStackNavigator({
  splash: {
    screen: SplashScreen,
    navigationOptions: {...navigationOptions}
  },
  login: {
    screen: LoginScreen,
    navigationOptions: {...navigationOptions}
  },
  home: {
    screen: HomeScreen,
    navigationOptions: {...navigationOptions}
  }
});

我现在在 PinEntry 屏幕中,我想回到TabNavigator中的交易标签。我可以使用以下脚本返回 Home 标签:

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

但我的目标是返回特定的标签位置,在这种情况下是' Transactions' 标签。

我已经搜索了很多次但没有解决方案,任何帮助都会受到赞赏。

1 个答案:

答案 0 :(得分:4)

我找到了解决方案:

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