在嵌套堆栈中重置 react-navigation 3
react-navigation 3 reset in nested stack
我试图了解如何在嵌套堆栈中重置
这是我的代码
const AuthStack = createStackNavigator(
{
Welcome,
Login,
Register,
ConfirmationCode,
},
{
initialRouteName: 'Welcome',
headerMode: 'none',
lazy: true,
transitionConfig,
defaultNavigationOptions: {
gesturesEnabled: false,
},
}
)
const AppStack = createStackNavigator(
{
TabStack,
SearchResult,
BusinessDetail,
BusinessMap,
MakeAppointment,
TermsAndConditions
},
{
initialRouteName: 'TabStack',
headerMode: 'none',
lazy: true,
transitionConfig,
defaultNavigationOptions: {
gesturesEnabled: false,
},
}
)
let MainStack = createSwitchNavigator(
{
AuthLoading,
Auth: AuthStack,
App: AppStack,
},
{
initialRouteName: 'AuthLoading',
headerMode: 'none',
lazy: true,
defaultNavigationOptions: {
gesturesEnabled: false,
},
}
)
TabStack
import React from 'react';
import { createBottomTabNavigator, createAppContainer } from 'react-navigation';
import {
Search,
MyFavourites,
MyAppointments,
UserProfile
} from '../screens'
import Icon from 'react-native-vector-icons/Feather';
import Colors from '../utils/Colors'
let TabStack = createBottomTabNavigator(
{
Search,
MyFavourites,
MyAppointments,
UserProfile,
},
initialRouteName: 'ScreenTab1',
tabBarOptions: {
activeTintColor: Colors.pink,
inactiveTintColor: Colors.black,
showLabel: false,
style: {
backgroundColor: 'white'
}
},
}
)
export default createAppContainer(TabStack);
我想了解如何进行重置,例如:
reset from UserProfile to TabStack (in AppStack) to AuthStack
我试过这样做
const resetAction = StackActions.reset({
index: 0,
actions: [NavigationActions.navigate({ routeName: 'AuthStack' })],
});
this.props.navigation.dispatch(resetAction);
或者这样
const resetAction = StackActions.reset({
index: 0,
key: null,
actions: [NavigationActions.navigate({ routeName: 'AuthStack' })],
});
this.props.navigation.dispatch(resetAction);
但是我得到了错误
there is no route defined for AuthStack
我在 Whosebug 中检查了问题,但那里的答案对我不起作用,总是向我显示我在上面写的相同错误。
尝试将其设置为 AppStack
,因为无论如何它都会重定向到 GeneralStack
,就像您在 AppStack
中的 initialRouteName
一样
const resetAction = StackActions.reset({
index: 0,
key: null,
actions: [NavigationActions.navigate({ routeName: 'App' })],
});
this.props.navigation.dispatch(resetAction);
您可以执行以下操作,重置为 authStack
、
创建重置操作如下,
const resetAction = StackActions.reset({
index: 0,
actions: [NavigationActions.navigate({ routeName: "AuthStack" })],
key: null
});
this.props.navigation.dispatch(resetAction);
并将 AuthStack 添加到 appStack 或您调用上述代码的堆栈中。
例如,如果您从您的应用程序堆栈调用它,请在您的应用程序堆栈中添加以下行作为路由
const AppStack = createStackNavigator(
{
TabStack,
SearchResult,
BusinessDetail,
BusinessMap,
MakeAppointment,
TermsAndConditions,
AuthStack <---Insert THIS
},
这对我用于注销时有用。
你的 resetAction
不成功,因为你在 TabStack
上发送它(因为你在 UserProfile
上调用 this.props.navigation.dispatch
,如果我没猜错的话)。您需要将 resetAction
发送给您的 MainStack
。 This thread here 建议了一些可以实现此目的的方法。而且,这是我的首选解决方案,因为我不必围绕导航器传递道具或使用它调用多个嵌套操作。
- 创建一个包含以下内容的
navigationService.js
(以保留您的顶级导航器作为参考)
import { NavigationActions, StackActions } from 'react-navigation';
let _navigator;
function setTopLevelNavigator(navigatorRef) {
_navigator = navigatorRef;
}
function navigateMainNavigator(routeName, params) {
_navigator.dispatch(
NavigationActions.navigate({
routeName,
params,
}),
);
}
// add other navigation functions that you need and export them
export default {
setTopLevelNavigator,
navigateMainNavigator,
};
- 在您的
App.js
或您呈现 MainStack
的任何其他文件上,执行此操作以设置引用
import NavigationService from './navigationService';
...
render() {
return (
...
<MainStack
ref={navigatorRef => {
NavigationService.setTopLevelNavigator(navigatorRef);
}}
/>
...
)
}
- 无论何时你想重置为你的
AuthStack
(或你的 MainStack
中的任何其他堆栈),只需导入 NavigationService
并调用
NavigationService.navigateAndReset('Auth', {...yourParamsIfAny});
// 'Auth' because you named it that way in your 'MainStack'
============================================= ==============================
已编辑
navigationService.js
中的先前解决方案适用于 StackNavigator
作为 MainStack
function navigateAndReset(routeName, params) {
_navigator.dispatch(
StackActions.reset({
index: 0,
actions: [
NavigationActions.navigate({
routeName,
params,
}),
],
})
);
}
我试图了解如何在嵌套堆栈中重置 这是我的代码
const AuthStack = createStackNavigator(
{
Welcome,
Login,
Register,
ConfirmationCode,
},
{
initialRouteName: 'Welcome',
headerMode: 'none',
lazy: true,
transitionConfig,
defaultNavigationOptions: {
gesturesEnabled: false,
},
}
)
const AppStack = createStackNavigator(
{
TabStack,
SearchResult,
BusinessDetail,
BusinessMap,
MakeAppointment,
TermsAndConditions
},
{
initialRouteName: 'TabStack',
headerMode: 'none',
lazy: true,
transitionConfig,
defaultNavigationOptions: {
gesturesEnabled: false,
},
}
)
let MainStack = createSwitchNavigator(
{
AuthLoading,
Auth: AuthStack,
App: AppStack,
},
{
initialRouteName: 'AuthLoading',
headerMode: 'none',
lazy: true,
defaultNavigationOptions: {
gesturesEnabled: false,
},
}
)
TabStack
import React from 'react';
import { createBottomTabNavigator, createAppContainer } from 'react-navigation';
import {
Search,
MyFavourites,
MyAppointments,
UserProfile
} from '../screens'
import Icon from 'react-native-vector-icons/Feather';
import Colors from '../utils/Colors'
let TabStack = createBottomTabNavigator(
{
Search,
MyFavourites,
MyAppointments,
UserProfile,
},
initialRouteName: 'ScreenTab1',
tabBarOptions: {
activeTintColor: Colors.pink,
inactiveTintColor: Colors.black,
showLabel: false,
style: {
backgroundColor: 'white'
}
},
}
)
export default createAppContainer(TabStack);
我想了解如何进行重置,例如:
reset from UserProfile to TabStack (in AppStack) to AuthStack
我试过这样做
const resetAction = StackActions.reset({
index: 0,
actions: [NavigationActions.navigate({ routeName: 'AuthStack' })],
});
this.props.navigation.dispatch(resetAction);
或者这样
const resetAction = StackActions.reset({
index: 0,
key: null,
actions: [NavigationActions.navigate({ routeName: 'AuthStack' })],
});
this.props.navigation.dispatch(resetAction);
但是我得到了错误
there is no route defined for AuthStack
我在 Whosebug 中检查了问题,但那里的答案对我不起作用,总是向我显示我在上面写的相同错误。
尝试将其设置为 AppStack
,因为无论如何它都会重定向到 GeneralStack
,就像您在 AppStack
initialRouteName
一样
const resetAction = StackActions.reset({
index: 0,
key: null,
actions: [NavigationActions.navigate({ routeName: 'App' })],
});
this.props.navigation.dispatch(resetAction);
您可以执行以下操作,重置为 authStack
、
创建重置操作如下,
const resetAction = StackActions.reset({
index: 0,
actions: [NavigationActions.navigate({ routeName: "AuthStack" })],
key: null
});
this.props.navigation.dispatch(resetAction);
并将 AuthStack 添加到 appStack 或您调用上述代码的堆栈中。
例如,如果您从您的应用程序堆栈调用它,请在您的应用程序堆栈中添加以下行作为路由
const AppStack = createStackNavigator(
{
TabStack,
SearchResult,
BusinessDetail,
BusinessMap,
MakeAppointment,
TermsAndConditions,
AuthStack <---Insert THIS
},
这对我用于注销时有用。
你的 resetAction
不成功,因为你在 TabStack
上发送它(因为你在 UserProfile
上调用 this.props.navigation.dispatch
,如果我没猜错的话)。您需要将 resetAction
发送给您的 MainStack
。 This thread here 建议了一些可以实现此目的的方法。而且,这是我的首选解决方案,因为我不必围绕导航器传递道具或使用它调用多个嵌套操作。
- 创建一个包含以下内容的
navigationService.js
(以保留您的顶级导航器作为参考)
import { NavigationActions, StackActions } from 'react-navigation';
let _navigator;
function setTopLevelNavigator(navigatorRef) {
_navigator = navigatorRef;
}
function navigateMainNavigator(routeName, params) {
_navigator.dispatch(
NavigationActions.navigate({
routeName,
params,
}),
);
}
// add other navigation functions that you need and export them
export default {
setTopLevelNavigator,
navigateMainNavigator,
};
- 在您的
App.js
或您呈现MainStack
的任何其他文件上,执行此操作以设置引用
import NavigationService from './navigationService';
...
render() {
return (
...
<MainStack
ref={navigatorRef => {
NavigationService.setTopLevelNavigator(navigatorRef);
}}
/>
...
)
}
- 无论何时你想重置为你的
AuthStack
(或你的MainStack
中的任何其他堆栈),只需导入NavigationService
并调用
NavigationService.navigateAndReset('Auth', {...yourParamsIfAny});
// 'Auth' because you named it that way in your 'MainStack'
============================================= ==============================
已编辑
navigationService.js
中的先前解决方案适用于 StackNavigator
作为 MainStack
function navigateAndReset(routeName, params) {
_navigator.dispatch(
StackActions.reset({
index: 0,
actions: [
NavigationActions.navigate({
routeName,
params,
}),
],
})
);
}