lecham
lecham

Reputation: 2464

React Navigation - Check on which screen the user is

I am using react-navigation in my project and I need to detect if the user is on the Dashboard/Graph/Posts page.

For example, if I am on Posts page, I need a param to write a conditional.

e.g. Make a request if I am only at Posts page

Is it possible to check on which screen the user is?

Upvotes: 0

Views: 586

Answers (2)

Ravi
Ravi

Reputation: 35569

You can get it through navigation object, try it

this.props.navigation.state.routeName 

Upvotes: 3

Jebin Benny
Jebin Benny

Reputation: 951

You can try the following,

on componentDidMount

componentDidMount() {
    this.subs = this.props.navigation.addListener("didFocus", payload => {
      console.log("PAY_LOAD...", payload);

     // Check the payload and your logic(you can get the current screen name like => payload.state.routeName)
    });
  }

on componentWillUnmount

componentWillUnmount() {
    this.subs.remove();
  }

Note: Don't forgot to remove listener on 'componentWillUnmount()'

Upvotes: 0

Related Questions