簡體   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