Got a form to submit a POST CRUD operation, with data save in a MySql.
I use react-hot-toast. Can anybody advise how to reset form's inputs on success? with simple toast i can do just .then(...) and i'm good, but there is a delay, no loading information. So i use promise based example. And now i have no idea how to setState("") if success, if error then do not clear. Or i could have a loading button and simple toast, but then again no idea how to sync them, or how to know data was written to update that button.
const onFormSubmit = async (e) => {
e.preventDefault()
try {
const body = {
title, //state - controlled input values
content,
}
toast.promise(
fetch('/api/post', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(body),
}),
{
loading: 'Saving...',
success: <b>Published!</b>,
error: <b>Could not save.</b>,
}
)
// .then(() => {
// setTitle('')
// setContent('')
// })
} catch (error) {
notifyNotOk()
}
}