I want to send a File in my request using React Js, but I am unable to append file in object of FormData using typescript.
Here’s my React Js Code
const [imageFile, setImageFile] = React.useState();
const [productImageMutation, productImageMutationResult]=useAddProductImageMutation();
const handleUploadClick = (e: any) => {
var file = e.target.files[0];
setImageFile(file);
};
const handleOnSubmit = (values: Picture) => {
const formData = new FormData();
formData.append("formFile", imageFile); //It Gives Error there.
productImageMutation({ data: formData});
};
>Solution :
Try to update your state initialization
const [imageFile, setImageFile] = React.useState('');
And also mention a type to you state because in TS you must provide a type
