我试图了解如何在嵌套堆栈中进行重置 这是我的代码
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);
但是我得到了错误
没有为AuthStack定义路由
我检查了stackoverflow中的问题,但是那里的答案对我不起作用,总是向我显示我上面写的相同错误。
答案 0 :(得分:1)
尝试将其设置为AppStack
,因为无论如何,它都将重定向到GeneralStack
,因为您在initialRouteName
中将其命名为AppStack
const resetAction = StackActions.reset({
index: 0,
key: null,
actions: [NavigationActions.navigate({ routeName: 'App' })],
});
this.props.navigation.dispatch(resetAction);
答案 1 :(得分:1)
您的resetAction
不成功,因为您是在TabStack
上调度它(因为如果我正确理解的话,是因为您正在this.props.navigation.dispatch
上调用UserProfile
)。您需要将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,
}),
],
})
);
}
答案 2 :(得分:-1)
您可以执行以下操作,以重置为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
},
在用于退出时,这对我有用。