【问题标题】:How to check if I can go back / use pop on react-native-navigation?如何检查我是否可以返回/在 react-native-navigation 上使用 pop?
【发布时间】:2017-12-20 23:00:14
【问题描述】:

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

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

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

【问题讨论】:

  • @Siggy 谢谢,但我的问题是针对react-native-navigation 库的react-native,而不是浏览器。为了更清楚,我已将其添加到标题中。

标签: react-native react-native-navigation


【解决方案1】:

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

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

【讨论】:

  • 谢谢!这确实解决了这个问题。我编辑添加了一个代码 sn-p。
【解决方案2】:

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

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2020-10-17
  • 1970-01-01
  • 2019-02-02
  • 1970-01-01
  • 1970-01-01
  • 2022-11-03
  • 1970-01-01
  • 2020-12-14
相关资源
最近更新 更多