0

问题是在加载选项卡导航器时会调用函数constructor()componentDidMount() 。每次用户到达名为myScreeen的屏幕时,我都需要调用函数,例如我们该怎么做?

4

2 回答 2

1

React-Navigation 提供了一个onNavigationStateChange道具,您可以使用它在导航状态更改时调用方法。屏幕跟踪示例向您展示了一个示例,您可以在其中获取新状态的键和路由名称。

从示例中可以看出,在getCurrentRouteName方法中,您可以在const route = navigationState.routes[navigationState.index];检索 之后,通过调用androute来找出键和路由名称。route.keyroute.routeName

于 2017-08-04T01:55:35.333 回答
-1

我发现了黑客:

    <Navigator
    onNavigationStateChange={(prevState, currentState) => {
  const getCurrentRouteName = (navigationState) => {
    if (!navigationState) return null;
    const route = navigationState.routes[navigationState.index];
    if (route.routes) return getCurrentRouteName(route);
    return route.routeName;
  };
  let currentRoute = getCurrentRouteName(currentState);
  if(currentRoute == "A") {
     console.log("TRIGGER")
}}/>;

然后您在组件中收听日志,如下所示:

  componentDidMount() {
    let slf = this;
    let log = console.log;
    console.log = function(txt) {
      if(txt == "TRIGGER") {
        slf.myFunction();  // CALL HERE YOUR FUNCTION
      } else {
        log.apply(console, arguments);
      }
    }
  }
于 2017-08-13T18:23:13.473 回答