How to trigger an event when a component is shown when using react-native-navigation?

前端 未结 6 1555
野趣味
野趣味 2021-02-02 09:35

I am using react-native with react-native-navigation. I would like to reload data when a component is shown. The component is shown when a user clicks on a tab navigation button

6条回答
  •  南笙
    南笙 (楼主)
    2021-02-02 09:52

    For RNN v3, after trying out for quite a couple times, I finally figured out the correct way:

      componentDidMount() {
        this.navigationEventListener = Navigation.events().bindComponent(this);
      }
    
      componentWillUnmount() {
        if (this.navigationEventListener) {
          this.navigationEventListener.remove();
        }
      }
    
      componentDidAppear() {   // Lazy loading data for RNN
        if (this.state.routes.length === 0) {
          this.getData();
        }
      }
    

    The key is that, the binding of event listener is mandatory, otherwise the componentDidAppear() and componentDidDisappear() won't be triggered.

提交回复
热议问题