简体   繁体   中英

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. How to after login the page will go to the Order List page? Currently after logging in, the page will go to the homepage

I think you can use goBack() method.

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.

Now, in your Login.js component file under the useEffect hook

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);
      } 
    }
}, []);

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