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

前端 未结 6 1547
野趣味
野趣味 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:59

    I like the solution proposed by Bruno Reis. I tweaked mine to make it a bit simpler.

    class Whatever extends Component {
        componentDidMount(){
            this.load()
            this.props.navigation.addListener('willFocus', this.load)
        }
        load = () => {
            ...
        }
    
    }
    

提交回复
热议问题