所以,有一个屏幕我正在使用javascript导航栏而不是react-native-navigation导航(因为this bug)。
我需要显示一个后退按钮,如果它可以返回(如果它不是堆栈中的第一个屏幕)。
有什么方法可以检查它是否可以返回?
类似于navigator.canBoBack()
或navigator.getCurrentScreenStack().lenght > 1
。
答案 0 :(得分:5)
检查屏幕的commandType
支柱是Push
还是ShowModal
并相应地调用设置按钮。
const canGoBack =
this.props.commandType === 'Push' ||
this.props.commandType === 'ShowModal'
答案 1 :(得分:0)
您可以跟踪您是否能够像这样返回:
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/react-navigation/issues/295