How to redirect previous location after signout and Login in react using useLocation hook?

763 Views Asked by At
import { Link, useLocation, useNavigate } from "react-router-dom";

I have to move previous location after login successfully but again and again it allow me to default url .

const Login = () => {
  const location = useLocation();
  let from = location.state?.from?.pathname || "/";
  if (user) {
    navigate(from, { replace: true });
  }

 I am expecting to get access previous location towhere i signout. 
1

There are 1 best solutions below

0
On

navigate(-1) will give you the result you wanted.

const Login = () => {
   signInWithEmailAndPassword(auth, email, password)
    .then((res) => {
      const user=res.user;
      navigate(-1);
    })
   .catch((err)=>{
    })
}