简体   繁体   中英

Reactjs handle browser refresh

How to handle page refresh in reactjs.Like I have a url :

/main/books/page

Currently on browser through react-router but when I refresh the page or put another url in address bar manually I want to have previous url in bar to get loaded not the new one after some conditions check.

Eg If a logged in user try to access /login or /signup routes then they get redirected to url they are comming from ie last or previous url they visited.

Basically my question is how to fetch previously visited url in browser before the browser is reloaded or redirected to another url manually?

You can check the authentication in the componentDidMount function and redirect user using history like this :

componentDidMount() {
  if(!this.props.autheticated) {
    this.props.history.push('/login')
  }
}

in login page,

componentDidMount(){
  if(isUserLoggedIn){
this.props.history.goBack()

} }

or use this.props.history.pop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM