I want to add to state and file object which I'm getting from input type file, and my problem is that I cannot update state with this:
currentTarget.files[0]
I'm getting this error:
DOMException: Failed to set the 'value' property on 'HTMLInputElement': This input element accepts a filename, which may only be programmatically set to the empty string.
const [data, changeData] = useState({
name: '',
surname: '',
cv: '',
});
HandleChangeEvent for getting data
const handleChangeData = ({ currentTarget }, key) => {
if (currentTarget.type === 'file') {
const files = currentTarget.files[0];
changeData((prevState) => {
console.log(prevState);
return { ...prevState, [key]: files };
});
} else {
// Validate property
const val = currentTarget.value;
const errorMessage = validateProperty(currentTarget);
if (errorMessage) errors[currentTarget.name] = errorMessage;
else delete errors[currentTarget.name];
changeData((prevState) => {
return { ...prevState, [key]: val };
});
}
};
I want to get property files
from input field and send it to backend