Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Link button making problems with redirect

I have a question. I have a Link element and a CustomButton element inside it which I have created and on that button I have onClick handler.

< Link to = "/dashboard"
style = {
    linkStyles
  } >
  <
  CustomButton text = "Login"
onClick = {
  handleSubmit
}
/> <
/Link>

This above is the code for the button and link

const handleSubmit = (e: any) => {
    if (email !== user.email || password !== user.password) {
      setError("Please make sure you have the correct email or password");
    } else {
      setError("");
    }
  };

And this is the handler. How can I make so when I click I dont get redirected immediately but instead let the handler show the error if the password or email are incorrect

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

can add a prevent default and useHistory to navigate

 const history = useHistory();

 const handleSubmit = (e: any) => {
       e.preventDefault();

    let timeout = 0

    if (email !== user.email || password !== user.password) {
      setError("Please make sure you have the correct email or password");
      timeout = 3000 // show the error for 3 seconds
    } else {
      setError("");
    }

    setTimeout(() => {history.push('/dashboard')}, timeout)
};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading