如何检查我是否可以返回/在 react-native-navigation 上使用 pop?

How to check if I can go back / use pop on react-native-navigation?

所以,有一个屏幕我正在使用 javascript 导航栏而不是 react-native-navigation 导航栏(因为 this bug)。

只有当返回按钮可以返回时(如果它不是堆栈中的第一个屏幕),我才需要显示返回按钮。

有什么方法可以检查它是否可以返回? 类似于 navigator.canBoBack()navigator.getCurrentScreenStack().lenght > 1.

您可以跟踪是否可以像这样返回:

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

检查屏幕的 commandType 属性是 Push 还是 ShowModal 并相应地调用设置按钮。

const canGoBack =
      this.props.commandType === 'Push' ||
      this.props.commandType === 'ShowModal'