所以,有一个屏幕我使用的是javascript navbar,而不是react-navigation-navigation One(因为此错误)。
我只有在可以返回的情况下显示一个返回按钮(如果不是堆栈中的第一个屏幕)。
有什么办法检查它是否可以返回?类似navigator.canBoBack()
或navigator.getCurrentScreenStack().lenght > 1
。
检查屏幕的commandType
Prop是否为Push
或ShowModal
,并相应地调用设置按钮。
const canGoBack =
this.props.commandType === 'Push' ||
this.props.commandType === 'ShowModal'
您可以跟踪如果您能够像这样返回:
const prevGetStateForAction = Navigator.router.getStateForAction;
Navigator.router.getStateForAction = (action, state) => {
// Do not allow to go back from Login
if (action.type === "Navigation/BACK" && state && state.routes[state.index].routeName === "Login") {
return null;
}
// Do not allow to go back to Login
if (action.type === "Navigation/BACK" && state) {
const newRoutes = state.routes.filter(r => r.routeName !== "Login");
const newIndex = newRoutes.length - 1;
return prevGetStateForAction(action, { index: newIndex, routes: newRoutes });
}
return prevGetStateForAction(action, state);
};
请参阅:https://github.com/react-community/reaect-navigation/issues/295