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
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.