这是我的项目文件层次结构 RootTabNavigator | AuthStackNavigator // I want to go back to this navigator | AuthoScreen | Welcome Screen | MainTabNavigator // I want to reset MainTabNavigator | FeedStacknavigator | Screen A | OtherS
RootTabNavigator | AuthStackNavigator // I want to go back to this navigator | AuthoScreen | Welcome Screen | MainTabNavigator // I want to reset MainTabNavigator | FeedStacknavigator | Screen A | OtherStackNavigatorOne | Screen E | OtherStackNavigatorTwo | Screen D | MenuStackNavigator | Menuo <-I'm here and want to reset to 'MainTabNavigator' and go BACK to 'AuthScreen' | Screen B | Screen C
问题
用户在MenuStackNavigator和MainTabNavigator下的Menuo屏幕上.
如果用户没有令牌(当用户注销时),则用户返回到Auth Screen.
但同时我想重置MainTabNavigator.您可以卸载,执行NavigationActions.init()或其他任何操作.我更喜欢NavigationActions.init()
我只是想将MainTabNavigator设置为第一次.
码
如果没有令牌,我会回到Auth Screen(这是有效的)
This code if the part of Menuo Screen componentWillReceiveProps(nextProps) { if ( nextProps.token == undefined || _.isNil(nextProps.token) ) { const backAction = NavigationActions.back({ key: null }) nextProps.navigation.dispatch(backAction); ...
(问题)我们如何重置MainTabNavigator,包括子StackNavigators?
MainTabNavigator.js export default TabNavigator( { Feed: { screen: FeedStacknavigator, }, OtherOne: { screen: OtherStackNavigatorOne, } ... }, { navigationOptions: ({navigation}) => ){ header: null, tabBarIcon: ({focused}) => ... ... }
可能解决方案
我可以将MainTabNavigator从函数更改为类,并在那里处理重置TabNavigator. (我不确定).
这一次,我需要一个具体的工作实例.我一直在阅读文档并申请我的应用程序,但我无法解决这个问题.
如果有任何不清楚的地方,请告诉我.
UPDATE
const RootTabNavigator = TabNavigator ({ Auth: { screen: AuthStackNavigator, }, Welcome: { screen: WelcomeScreen, }, Main: { screen: MainTabNavigator, }, }, { navigationOptions: () => ({ ... } ); export default class RootNavigator extends React.Component { componentDidMount() { this._notificationSubscription = this._registerForPushNotifications(); }这应该适用于大多数情况:
componentWillReceiveProps(nextProps) { if ( nextProps.token == undefined || _.isNil(nextProps.token) ) { let action = NavigationActions.reset({ index: 0, key: null, actions: [ NavigationActions.navigate({routeName: 'Auth'}) ] }); nextProps.navigation.dispatch(action); } ... }
或者尝试使用自定义操作增强导航器:
const changeAppNavigator = Navigator => { const router = Navigator.router; const defaultGetStateForAction = router.getStateForAction; router.getStateForAction = (action, state) => { if (state && action.type === "RESET_TO_AUTH") { let payLoad = { index: 0, key: null, actions: [NavigationActions.navigate({routeName: "AuthStackNavigator"})] }; return defaultGetStateForAction(NavigationActions.reset(payLoad), state); // or this might work for you, not sure: // return defaultGetStateForAction(NavigationActions.init(), state) } return defaultGetStateForAction(action, state); }; return Navigator; }; const screens = { ... } RootTabNavigator = changeAppNavigator(TabNavigator(screens, { initialRouteName: ..., ... }));
然后在您的Menuo屏幕中执行:
componentWillReceiveProps(nextProps) { if ( nextProps.token == undefined || _.isNil(nextProps.token) ) { nextProps.navigation.dispatch({type: "RESET_TO_AUTH"}); ...