0

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

Bon Pham
  • 1
  • 1

2 Answers2

0

I think you can use goBack() method.

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

and after logging in

history.goBack();
User
  • 83
  • 7
0

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);
      } 
    }
}, []);
Dharman
  • 21,838
  • 18
  • 57
  • 107