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

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

    include this as 'callIfBackToThisRoute'...

    export default ( props, call ) => {
        if( !props.navigation ) throw 'I need props.navigation'
        const thisRoute = props.navigation.state.routeName;
        props.navigation.addListener(
            'willFocus',
            payload => {
                if( payload.state.routeName == thisRoute) call(props)
            }
        );
    }
    

    and use it inside your component...

    componentDidMount() {
        const { doIt } = this.props;
        doIt()
        callIfBackToThisRoute(
            this.props,
            (props) => doIt()
        )
    }
    

提交回复
热议问题