I have created a form submit method that works correctly however when I click submit the form data stays in the field boxes. I have added new set state values below the axios post request but now I am getting … is not a function error. What is the cleanest way to clear forms after submit in React? I don’t want to add any more npm’s like Formik or Redux-Form as I have enough already.
Also should the alert submit success message be on the front-end? How would it know if the details are saved correcly?
// state for the current field value
const [article, setArticle] = useState({
articleTitle: ``,
articleTypeID: ``,
articleContent: ``,
// photos: undefined,
userID: props.user.userID,
error: ``,
});
const [photos, setPhotos] = useState({
photos: null
})
....... handleChange, useSelector methods etc
function handleSubmitArticle(e: React.FormEvent<HTMLFormElement>) {
e.preventDefault();
const formData = new FormData();
formData.append("articleTitle", article.articleTitle);
formData.append("articleContent", article.articleContent);
formData.append("userID", article.userID);
formData.append("articleTypeID", article.articleTypeID);
formData.append("photos", photos);
const config = {
headers: {
'content-type': 'multipart/form-data'
}
};
axios.post("http://localhost:5002/api/article/createarticle", formData, config);
window.render()
}
..... form input materialUI fields etc
I have been trying to get a window.render function to work as I think this looks the easiest way but I keep getting the “is not a function” error. Do I need to add something to my index.js file for re-rendering windows? (like window.render = () => ).