Search code examples
react-nativereact-navigationreact-native-navigationreact-navigation-stack

React Navigation previous screen knows when it is activated


I'm trying to figure out a way to call a function in my previous screen if it is revisited.

  1. Currently on Screen 1
  2. Click a Button (Goes to Screen 2)
  3. Click Back (Goes Back to Screen 1)

this.props.navigation.dispatch(NavigationActions.back())

  1. Function on Screen 1 is called. I don't need to pass any parameters or anything I just need to know when the screen has been called again. I'd rather not clear the entire state and reset it if I don't have t.

Solution

  • You can use didFocus or willFocus listener, please check:

    https://reactnavigation.org/docs/en/navigation-prop.html#addlistener-subscribe-to-updates-to-navigation-lifecycle