简体   繁体   English

Reactjs 处理浏览器刷新

[英]Reactjs handle browser refresh

How to handle page refresh in reactjs.Like I have a url :如何在 reactjs 中处理页面刷新。就像我有一个 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.目前通过 react-router 在浏览器上,但是当我刷新页面或手动在地址栏中放置另一个 url 时,我希望在某些条件检查后加载栏中以前的 url,而不是新的。

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.例如,如果登录用户尝试访问 /login 或 /signup 路由,那么他们将被重定向到他们来自的 url,即他们访问的最后一个或上一个 url。

Basically my question is how to fetch previously visited url in browser before the browser is reloaded or redirected to another url manually?基本上我的问题是如何在浏览器重新加载或手动重定向到另一个 url 之前在浏览器中获取以前访问过的 url?

You can check the authentication in the componentDidMount function and redirect user using history like this :您可以在componentDidMount函数中检查身份验证并使用如下history重定向用户:

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()或使用 this.props.history.pop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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