Search code examples
reactjsroutesscreennativenavigator

React Native - Stack Navigator Check from which screen has the new panel been opened


I am using Stack Navigator for navigating between screens. I have a Info screen which can be accessed from two other different screens in my app. Depending from which screen I have tried to access my Info panel, different text should be displayed on it. How can I check which was the screen before the current one(the info screen)?

I tried playing with this.props.navigation.state.routeName. However, it would only return the name of the current panel, not the one before. Is there any way I can check the previous one or at least get its' name?


Solution

  • You can pass the name of the screen in params from which you're navigating to info screen

    let { routeName } = this.props.navigation.state;
    
    this.props.navigation.navigate('info', {
      lastScreen: routeName 
    });
    

    and get the name of prev. screen in info screen like so

    const { lastScreen } = this.props.navigation.state.params;
    ....
    

    I hope it helps.

    Read more about Passing parameters to routes