I have added a form in my app. When I send the form data to my local server I create an image with a title. When I attach images I should be using input type="file"
. Also, I should use FormData in my app.
But when I write my component I have an error 'formElem' is not defined
in the two lines commented in the code. How do I fix it?
Here is my component code:
const AddImage = (props) => {
formElem.onsubmit = async (e) => { // ERROR THERE
e.preventDefault();
try {
const response = await api(`${imageRoute}`, {
method:'POST',
body: new FormData(formElem), // ERROR THERE
});
} catch(e) {
console.error(e);
}
};
return (
<div className="formImage">
<form id="formElem">
<input type="text" name="name" value=""/>
<input type="text" name="surname" value=""/>
<input type="file" name="picture" accept="image/*"/>
<div className="formButtonImage">
<button type="submit">Add</button>
</div>
</form>
</div>
);
};