简体   繁体   English

登录后重定向网站路径,不是首页reactjs

[英]Redirect the website path after logging in, not the homepage reactjs

The path from the email to the user's Order List page, if not logged in, will go to the login page.从 email 到用户的订单列表页面的路径,如果没有登录,将 go 到登录页面。 How to after login the page will go to the Order List page?登录页面后如何将 go 到订单列表页面? Currently after logging in, the page will go to the homepage目前登录后页面会go到首页

I think you can use goBack() method.我认为您可以使用goBack()方法。

import { useHistory } from 'react-router-dom'
const history = useHistory()

and after logging in并在登录后

history.goBack();

Hope this helps your problem希望这可以帮助您解决问题

So for the login button do this所以对于登录按钮执行此操作

<button>
   <Link
     to={{
        pathname: '/login',  // this pathname should be equal to the route that you have defined for Login page
        state: { from: 'cart' },
     }}
    >
    Login to see order list
    </Link>
</button>

So the above code snippet should go to be page which check if the user is logged in or not and redirect them to the login page if they are not initially logged in.所以上面的代码片段应该 go 是检查用户是否登录的页面,如果他们最初没有登录,则将他们重定向到登录页面。

Now, in your Login.js component file under the useEffect hook现在,在useEffect挂钩下的 Login.js 组件文件中

useEffect(() => {
    if (user is logged in condition) {
      let intended = history.location.state; // this history object comes react-router-dom
      if (intended) {
        history.push(intended.from);
      } 
    }
}, []);

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

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