react-navigation createBottomTabNavigator 刷新問題


RN使用 createBottomTabNavigator 創建底部導航欄時,只會在第一次創建時調用 componentDidMount 鈎子,當跳轉到另一個頁面,點擊返回鍵再次返回到此頁面時不會再次調用,但我們往往需要在返回此頁面后重新執行獲取數據等方法函數。

解決方法

componentDidMount() {
    this._navListener = this.props.navigation.addListener('didFocus', () => {
      this.getData();
    });
  }

  componentWillUnmount() {
    this._navListener.remove();
  }

監聽路由,即使頁面沒有被重新渲染,當路由 didFocus 時,也會執行回調函數。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM