0

I'm doing user login and logout and after login or logout i am just changing pathname to homepage using this.props.history.push({pathname: '/', state: { url: this.props.location.pathname }});. Now when i redirect to that page the data is not updating, i have refresh the page to update the data. I am calling below code to update the data.

  componentDidMount(){
    let data = AuthService.fetchUserObj();
    console.log(data)
    this.setState({user: data})
  }

Please assist me how to automatically update user data to state once pathname changed after login.

Indrakant
  • 361
  • 3
  • 9
  • Are you persisting the state somehow in the app? If you are pushing to a new page how does that component know about the state you set on the previous component? – displacedtexan Jun 05 '20 at 17:45
  • You can pass state through react router to get on the other side: https://stackoverflow.com/questions/41466055/how-do-i-pass-state-through-react-router – displacedtexan Jun 05 '20 at 17:46

1 Answers1

1

It's just an assumption but maybe fetchUser is an asynchronous method. If it's so and method returns a promise then it has to be something like:

componentDidMount(){
    AuthService.fetchUserObj().then(data => {
        this.setState({user: data});
    }
}
Ken Bekov
  • 13,696
  • 3
  • 36
  • 44