I can't get the redirect using URLSearchParams

36 views Asked by At

I'm trying to get the redirect option in '/login?redirect=/envio' but it always fail. I'm using new URLSearchParams(buscar).get('redirect') and when I use log to see if I got the redirect, i recibe null. Can someone help?

This is de URL I'm creating

const pagarHandler = () => {
   navigate('/login?redirect=/envio');
}

This is the code

    const navigate = useNavigate();
    const buscar= useLocation();
    console.log(buscar);
    const redirectEnUrl = new URLSearchParams(buscar).get('redirect');
    console.log(redirectEnUrl);
    const redirect = redirectEnUrl ? redirectEnUrl : '/';
    console.log(redirect);

When the login is done, I use navigate to see if it's just a login or it was redirected to login because there wasan't a user, but it always redirects me to the home screen

navigate(redirect || ('/'));

Thanks and sorry for my English

EDIT: Okay so I found a solution for anyone looking here with the same problem. Insted of using the useLocation(), I used this and it worked

const urlParams = window.location.search;
const redirectEnUrl = urlParams.get('redirect')
0

There are 0 answers