如何检查我是否可以返回/使用反应原生导航的弹出窗口?

问题描述 投票:3回答:2

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

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

有什么方法可以检查它是否可以返回?像navigator.canBoBack()navigator.getCurrentScreenStack().lenght > 1之类的东西。

react-native react-native-navigation
2个回答
5
投票

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.