本文介绍了React Navigation:如何从堆栈返回到另一个堆栈中的屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 React Native 中使用 react-navigation v2,每当我想回到根导航器中的路由时都会被卡住.
I'm using react-navigation v2 in React Native and was stucked whenever I wanna go back to a route in the root navigator.
我有以下路由堆栈:
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}
}
});
我现在在 Home
路线中,我想注销并返回到 login
路线.我尝试使用以下代码:
I'm now in Home
route and I want to log out and go back to login
route. I tried with following code:
_goBackToLogin() {
const resetAction = StackActions.reset({
index: 0,
actions: [NavigationActions.navigate({ routeName: 'Home' })]
});
const goToLogin = NavigationActions.navigate({
routeName: 'login'
});
this.props.navigation.dispatch(resetAction);
this.props.navigation.dispatch(goToLogin);
}
但是,我没有结果,它仍然停留在Home
路线.我的代码有什么问题?
But, I got no result, it still stay in Home
route. What's wrong with my code?
推荐答案
您是否将导航器 Tab 和 Routers 合并为一个导航器,如下所示
Did you combine the navigators Tab and Routers into single navigator as below
const Routers = createStackNavigator({
splash: {
screen: SplashScreen,
navigationOptions: {...navigationOptions}
},
login: {
screen: LoginScreen,
navigationOptions: {...navigationOptions}
},
home: {
screen: HomeScreen,
navigationOptions: {...navigationOptions}
},
Tab: Tab
});
_goBackToLogin 与您的相同.
_goBackToLogin is same as yours.
这篇关于React Navigation:如何从堆栈返回到另一个堆栈中的屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!