繁体   English   中英

在 TabNavigator 中到达屏幕时如何启动功能? [反应导航]

[英]How to launch function when screen in reached in TabNavigator ? [REACT NAVIGATION]

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

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

从示例中,在getCurrentRouteName方法中,您可以执行const route = navigationState.routes[navigationState.index]; 一旦你检索了route ,你可以通过调用route.keyroute.routeName来找出密钥和路由名称。

我发现黑客:

    <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);
      }
    }
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM