最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React Navigation: How to go back to root Tab Navigator with specific tab from child stack - Stack Overflow

programmeradmin3浏览0评论

I'm using react-navigation v2 in React Native and get stuck whenever want to go back to a specific tab in the root navigator.

I've the following route stack:

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}
  }
});

I'm now in PinEntry screen and I want to go back to Transactions tab in TabNavigator. I can go back to Home tab using the following script:

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

But my goal is to go back to specific tab position, in this case is 'Transactions' tab.

I've googled it so many times but with no solution, any help would be appreciated.

I'm using react-navigation v2 in React Native and get stuck whenever want to go back to a specific tab in the root navigator.

I've the following route stack:

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}
  }
});

I'm now in PinEntry screen and I want to go back to Transactions tab in TabNavigator. I can go back to Home tab using the following script:

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

But my goal is to go back to specific tab position, in this case is 'Transactions' tab.

I've googled it so many times but with no solution, any help would be appreciated.

Share Improve this question asked Jun 9, 2018 at 12:40 Justinus HermawanJustinus Hermawan 1,2141 gold badge26 silver badges47 bronze badges 4
  • this.props.navigation.navigate('Transactions') without reset stack works correctly for me... – Ali SabziNezhad Commented Jun 9, 2018 at 13:05
  • @ali-sn Yes, it works, but I need to reset my stack to that screen. How to do it with reset stack? – Justinus Hermawan Commented Jun 9, 2018 at 13:10
  • I will tell you if i found a solution...! :-D – Ali SabziNezhad Commented Jun 9, 2018 at 13:26
  • @ali-sn I found the solution, using 2 dispatches. – Justinus Hermawan Commented Jun 9, 2018 at 13:30
Add a ment  | 

1 Answer 1

Reset to default 8

I found the solution:

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);

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论