I'm trying to set use setstate inside a function which is called on form submit, I am also using preventDefault:
const [ucasTariff, setUcasTariff] = useState<string>('');
const handleSubmit = async (e: FormEvent) => {
e.preventDefault();
const target = e.target as typeof e.target & {
ucasPoints: {value: string};
};
setUcasTariff(target.ucasPoints.value);
}
logging the target.ucasPoints.value
prints the correct value but logging the value of ucasTariff
gives <empty string>
This is how I am calling the function in the <form>
element:
onSubmit={(e) => handleSubmit(e)}
How can I set the value properly?
Edit: forgot to mention that actually it does get set but only on the second time I click my submit button.
Edit 2: the suggested question is somewhat helpful in understanding what is going on but doesn't answer the question because following those answers does not solve the issue but creates new ones when trying to send my axios request. So I am still looking for an answer.