I'm using react-router-dom v6 and I'm accessing the from
value from the location
object and it gives the pathname
but when executing navigate(from,{replace:true}) it does not work.
const navigate = useNavigate();
const { state } = useLocation();
const from = state ? state.from.pathname : '/';
const [isDone, setIsDone] = useState(false);
useEffect(() => {
if (isDone) {
navigate(from, { replace: true }); //not working
}
}, [isDone]);
const Submit = async (e) => {
e.preventDefault();
let data = { email, password };
if (!email || !password) {
setMessage('Please Enter All Fields');
} else {
setLoading(true);
return await axios
.post('/signin', data)
.then((res) => {
if (res.data.message === 'Invalid Credentials') {
setMessage('Invalid Credentials');
}
if (res.data.message === 'Logged In') {
setIsDone(true);
}
})
.catch((err) => {
console.log(err);
})
.finally(() => {
setLoading(false);
});
}