react-navigation createBottomTabNavigator 刷新问题

RN使用 createBottomTabNavigator 创建底部导航栏时,只会在第一次创建时调用 componentDidMount 钩子,当跳转到另一个页面,点击返回键再次返回到此页面时不会再次调用,但我们往往需要在返回此页面后重新执行获取数据等方法函数。

解决方法

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

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

监听路由,即使页面没有被重新渲染,当路由 didFocus 时,也会执行回调函数。

posted @ 2019-04-15 20:56  xlupc  阅读(2431)  评论(0编辑  收藏  举报