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

前端 未结 6 1546
野趣味
野趣味 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条回答
  •  -上瘾入骨i
    2021-02-02 09:44

    This is what I ended up using:

    export default class RootNavigator extends React.Component {
      state = {currentScreen: null}
    
      _onNavigationStateChange(prevState, newState, action) {
        // const currentScreen = getCurrentRouteName(currentState)
        // const prevScreen = getCurrentRouteName(prevState)
        // console.debug('onNavigationStateChange currentScreen=', currentScreen,
        //   'prevScreen=', prevScreen, 'action.routeName=', action.routeName)
        console.debug('onNavigationStateChange action.routeName=', action.routeName)
        this.setState({currentScreen: action.routeName})
      }
    
      render() {
        return ;
      }
    

    coupled with componentDidUpdate() on the screen (which may or may not perform something depending on the screenProps's currentScreen).

    Note: I don't know what/where getCurrentRouteName() is and it gave error for me so I don't use it and use action.routeName directly.

    See https://github.com/react-community/react-navigation/issues/2371 and https://github.com/react-community/react-navigation/issues/51#issuecomment-323536642 for more information and discussion.

提交回复
热议问题