这是我的项目文件层次结构
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下在菜单屏幕上。
如果用户没有令牌(用户注销时),则用户返回验证屏幕。
,但与此同时,我想重置Maintabnavigator 。您可以卸载,执行NavigationActions.Init()或任何可能。我更喜欢navigations.init()
我只想将Maintabnavigator设置为第一次。
代码
如果没有令牌,我会回到auth屏幕(这正在工作)
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);
...
(问题)我们如何重置包括儿童stacknavigator的维护abnavigator?
MainTabNavigator.js
export default TabNavigator(
{
Feed: {
screen: FeedStacknavigator,
},
OtherOne: {
screen: OtherStackNavigatorOne,
}
...
}, {
navigationOptions: ({navigation}) => ){
header: null,
tabBarIcon: ({focused}) => ...
...
}
可能的解决方案
我也许可以将Mainterabnavigator从功能更改为类,并在那里处理重置TABNAVIGATOR。(我不知道)。
这次,我需要一个具体的工作示例。我一直在阅读DOC并申请我的应用程序,但无法解决。
请让我知道是否有什么不清楚的。
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 Screen
中:
componentWillReceiveProps(nextProps) {
if ( nextProps.token == undefined || _.isNil(nextProps.token) ) {
nextProps.navigation.dispatch({type: "RESET_TO_AUTH"});
...
您需要的是将导航器初始化为初始状态。您可以使用NavigationActions.init()
做到这一点。您可以在此处了解有关导航操作的更多信息。
您可以通过创建自定义导航操作,在此处阅读有关它们的更多信息。
这是一些代码可以为您做到这一点:
// First get a hold of your navigator
const navigator = ...
// Get the original handler
const defaultGetStateForAction = navigator.router.getStateForAction
// Then hook into the router handler
navigator.router.getStateForAction = (action, state) => {
if (action.type === 'MyCompleteReset') {
// For your custom action, reset it all
return defaultGetStateForAction(NavigationActions.init())
}
// Handle all other actions with the default handler
return defaultGetStateForAction(action, state)
}
为了触发您的自定义导航操作,您必须按照以下反应组件进行分配:
: this.props.navigation.dispatch({
type: "MyCompleteReset",
index: 0
})
您可以通过扩展路由器来定义自定义导航逻辑。为了完成您想要在问题层次结构中所描述的问题,您可以在下面做类似的事情。
Maintabnavigator.js
...
RootTabNavigator.router.getStateForAction = (action, state) => {
if (state && action.type === 'GoToAuthScreen') {
return {
...state,
index: 0,
};
}
return RootTabNavigator.router.getStateForAction(action, state);
};
MainTabNavigator.router.getStateForAction = (action, state) => {
if (state && action.type === 'GoToAuthScreen') {
return {
...state,
index: 0,
};
}
return MainTabNavigator.router.getStateForAction(action, state);
};
MenuStackNavigator.router.getStateForAction = (action, state) => {
if (state && action.type === 'GoToAuthScreen') {
return {
...state,
index: 0,
};
}
return MenuStackNavigator.router.getStateForAction(action, state);
};
在菜单屏幕文件中
componentWillReceiveProps(nextProps) {
if ( nextProps.token == undefined || _.isNil(nextProps.token) ) {
const goToAuthScreen = () => ({
type: 'GoToAuthScreen',
});
nextProps.navigation.dispatch(goToAuthScreen);
...
}
}