How Can First Setstate And Then Run Onsubmit Function
I have a object from another component and I want to add objects value to my Data but when I click on submit first time the console.log show empty And next time it's okay. How can
Solution 1:
The problem was solved by using an submitState in useEffect. By specifying a condition in the useEffect
const [startOnSubmit, changeOnSubmit] = useState(false)
useEffect(() => {
if (startOnSubmit) {
addPost(formData);
changeOnSubmit(false);
}
}, [formData, addPost, startOnSubmit, changeOnSubmit]);
const onSubmit = e => {
e.preventDefault();
const oto = { ...options, ...newOptions }
setFormData({
...formData,
options: oto,
category: selected === null ? '' : selected.id
});
changeOnSubmit(true)
}
Post a Comment for "How Can First Setstate And Then Run Onsubmit Function"