Home > database >  How to Navigate to separate URL in react router Dom v6?
How to Navigate to separate URL in react router Dom v6?

Time:11-26

i am making a validation form , with a function that checks for password and email , if true it should navigate to a separate website (not to a path within the web). But it is not working

This is my code below:

function validate() {
    if (email==='[email protected]' && password==="123456"){
        console.log(email);
 history('http://www.dummy.com/webmail')
}
}

return{
.
.
.
<Button className={stylesAuth.submitButton} variant="warning" onClick={validate}>
}

How should I do so?

CodePudding user response:

Since you're redirecting to an external website, you can simply use plain old JavaScript and do a:

window.location.href = "https://www.dummy.com/webmail";

For more information, see https://developer.mozilla.org/en-US/docs/Web/API/Location/href.

A little bit more information: using history doesn't make sense in this case, it is only meant for internal navigation within your web application. In fact, you cannot even push an external URL onto the history stack:

The new URL must be of the same origin as the current URL; otherwise, pushState() will throw an exception. https://developer.mozilla.org/en-US/docs/Web/API/History/pushState

  • Related