GIV
GIV

Reputation: 425

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?

Upvotes: 3

Views: 5282

Answers (1)

Ahsan Ali
Ahsan Ali

Reputation: 5135

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

Upvotes: 7

Related Questions