I've trying to redirect to Home Page when i click the button "Login" on my react studies, but i have no idea how to do it, because i used all the "redirects" possibles but no one worked.
The function below the is called when i click the button "login":
Here is my react router: the "Principal" page is the home
My code is in portuguese.
I tryed to use the "redirect("/principal");", and other functions but don't worked.
somebody can help me?
CodePudding user response:
To use redirect after login, you can use the useNavigate
hook from react-router-dom.
you would use it as below
import {useNavigate} from 'react-router-dom'
const navigate = useNavigate()
if(true){
navigate('/principal')
}
Simply replace the true condition, with your original if statement conditon
CodePudding user response:
You can use the hooks useNavigate from react-router
import { useNavigate } from "react-router-dom";
const LoginButton = () => {
const navigate = useNavigate();
function handleClick() {
// Your login function
navigate("/principal");
}
return (
<button type="button" onClick={handleClick}>
Login
</button>
);
}