I am trying to reset my form when a user clicks the submit button. The problem I am currently having is that the setFunction for my useState is not updating the form values on form submission. Below is the code.
const [formData, setFormData] = useState({
date: "", totalincome: "", sidehustleincome: "", stockincome: "", other: ""
})
const handleChange = (event) => {
const {name, value} = event.target;
setFormData({
...formData,
[name]: value
});
};
const handleClick = async (event) => {
event.preventDefault()
try {
await fetch(`${import.meta.env.VITE_API_URL}/data/income`, {
method: "POST",
body: JSON.stringify(formData),
headers: {
"Content-Type": "application/json",
}
});
**setFormData({
date: "", totalincome: "", sidehustleincome: "", stockincome: "", other: ""
}); **
} catch (err) {
console.log("An Error has ocurred", err);
}
}
return (
<div>
<p>Add Income</p>
<form className="box">
<div className="field">
<label className="label" for="date">Date:</label>
<div className="control">
<input className="input" type="date" id="date" name="date" value={formData.date} onChange={handleChange}/>
</div>
</div>
<div className="field">
<label className="label" for="totalincome">Total Income:</label><br />
<div className="control">
<input className="input" type="text" id="totalincome" name="totalincome" value={formData.totalincome} onChange={handleChange}/><br />
</div>
</div>
<div className="field">
<label className="label" for="sidehustleincome">Side Hustle Income:</label><br />
<div className="control">
<input className="input" type="text" id="sidehustleincome" name="sidehustleincome" value={formData.sidehustleincome} onChange={handleChange}></input><br />
</div>
</div>
<div className="field">
<label className="label" for="stockincome">Stock Income:</label><br />
<div className="control">
<input className="input" type="text" id="stockincome" name="stockincome" value={formData.stockincome} onChange={handleChange}></input><br />
</div>
</div>
<div className="field">
<label className="label" for="other">Other:</label><br />
<div className="control">
<input className="input" type="text" id="other" name="other" value={formData.other} onChange={handleChange}></input ><br />
</div>
</div>
<input className="button is-primary" type="submit" **onClick**={handleClick}/>
</form>
</div>
)
}
My desired functionality is for the page not to reset when the form is sent. I have tried running this code with syntax that should work, and it behaves like there is not a setFunction being called.
const handleClick = async (event) => {
**event.preventDefault()**
try {
await fetch(`${import.meta.env.VITE_API_URL}/data/income`, {
method: "POST",
body: JSON.stringify(formData),
headers: {
"Content-Type": "application/json",
}
});
**setFormData({
date: "", totalincome: "", sidehustleincome: "", stockincome: "", other: ""
}); **
} catch (err) {
console.log("An Error has ocurred", err);
}
}
I’ve noticed that the only way to get my code to work properly is to temporarily change the event handler from onClick to onSubmit, save the file, test the functionality, and then switch the event handler back to onClick, saving the file once again.
What is causing this change in functionality? Additionally, why isn’t useState being reset correctly after it’s called before switching the event handler between onClick and onSubmit and saving the file?