我在 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 中的"交易"选项卡。我可以使用以下脚本返回"主页"选项卡:
const resetAction = StackActions.reset({
index: 0,
actions: [NavigationActions.navigate('Home')]
});
this.props.navigation.dispatch(resetAction);
但我的目标是回到特定的选项卡位置,在本例中是"交易">选项卡。
我已经用谷歌搜索了很多次,但没有解决方案,任何帮助将不胜感激。
我找到了解决方案:
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);