I'm simply attempting to call a function submitformdata(data), which submits data to a database and returns a successRegister string on completion. Upon the return of this success string I want to navigate straight to the login page without further user interaction. What I have below runs without error, but it does not navigate to the \Login page even when the toLogin variable is set to successRegister verified by a console log statement. I expect it is something conceptual I am missing regarding how UI states are updated.
export default function Register() {
const [toLogin, setToLogin] = React.useState("failRegister");
const handleSubmit = (event) => {
event.preventDefault();
const data = new FormData(event.currentTarget);
if (data.get('email') === '' || data.get('password') === '' || data.get('verifypassword') === '' || data.get('firstName') === '' || data.get('lastName') === '') {
return
}
submitformdata(data).then(response => {
setToLogin(response);
if (toLogin === "successRegister") {
return <Navigate to="/Login" />;
}
});
};
return (code here for the Register page);
The first thing is that the value of "toLogin" does not change immediately. Please re-check the concept of hook here https://reactjs.org/docs/hooks-state.html
To update the UI, you need to use the "toLogin" variable in your return code

If you are using react-router-dom for routing then you can use useNavigate() hook as shown below but still it depends upon the version of react-router-dom.
And for you information useState hooks are asynchronous and everytime you update the state variables react will re-render the component thus rather than including <Navigate to="/login"> inside function you can include in render component itself.
{stateVar & }.
import { useNavigate } from "react-router-dom";
function Invoices() {
let navigate = useNavigate();
return (
<div>
<NewInvoiceForm
onSubmit={async (event) => {
let newInvoice = await createInvoice(
event.target
);
navigate(`/invoices/${newInvoice.id}`);
}}
/>
</div>
);
}
There are a few things you must consider when using react hooks. First do I need to use a react hook or can I use a plain variable? If you can use a plain variable and you don't need persistence between renders, then use a variable.
If you must have persistence between renders, then use a hook!
The code you provided doesn't use the toLogin state hook at all, and doesn't require it to be persistent between renders! With the logic encountered above we can see that there is no real reason or need for this hook in this case. Check out the code below.
import { useNavigate } from 'react-router-dom'; //using v6 here.
...
//No hooks required for persistence between renders.
//You can use a hook if you wish to handle errors and display them.
const navigate = useNavigate();
const handleSubmit = (event) => {
event.preventDefault();
const data = new FormData(event.currentTarget);
if (data.get('email') === '' || data.get('password') === '' || data.get('verifypassword') === '' || data.get('firstName') === '' || data.get('lastName') === '') {
return
}
submitformdata(data).then(response => {
//In this case the response must be "successRegister"
if (response === "successRegister") {
//returning <Navigate to="/Login" /> doesn't work because it is the return of the onSubmit function.
navigate("/Login");
//No return needed for this function.
}
});
};
return (code here for the Register page);