Given the following code:
const handleSubmit = (event) => {
event.preventDefault();
console.log("telFormValues when submit:"+JSON.stringify(telFormValues));
setFormValues({
...formValues,
"telefonos_empresa": telFormValues,
"emails_empresa": emailFormValues
});
Which is part of a form using TextFields with onChange, when text is placed in a input and I click the submit button directly,(handleSubmit is called) the console.log for telFormValues
shows the data from the form correctly, however, formValues
does not register the last input, unless I click out of it. It happens with any input in the form if it is the last one, if the user clicks submit directly, is not 'saved'. I don't understand why is this behaviour happening and after all, the console shows that the value is indeed there, but for some reason it is not setting on setFormValues
.
console.log(JSON.stringify(formValues["telefonos_empresa"]))
shows: [{"tel_identificator":"test1","tel":"234"}]
console.log(formValues)
(placed right in the following line) shows: telefonos_empresa: []
Edit: Per suggestion, tried:
const handleSubmit = (event) => {
event.preventDefault();
console.log("telFormValues en submit:"+JSON.stringify(telFormValues));
setFormValues({
...formValues,
"telefonos_empresa": telFormValues,
"emails_empresa": emailFormValues
},
console.log(JSON.stringify(formValues["telefonos_empresa"])),
console.log(formValues)
);
};
But the result is the same.