I am working on Nextjs and try to upload a single file. Every time I attach new file, then I get this error at the console log.
At the console, I see this error:
Warning: A component is changing an uncontrolled input of type file to be controlled. Input elements should not switch from uncontrolled to controlled (or vice versa). Decide between using a controlled or uncontrolled input element for the lifetime of the component.
Then, the second one is:
react-dom.development.js:2592 Uncaught 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.
Here is my sample code:
- HTML path at render function
<div className="form-group">
<label htmlFor="p_file">Image</label>
<input
id="p_file"
type="file"
className="form-control-file"
value={formData.image}
onChange={ e => onChangeImage(e)} />
</div>
- Javascript path
const [formData, setFormData] = useState({
...,
image: undefined
});
const onChangeImage = event => {
console.log(event.target.files[0]);
setFormData( prev => ({
...prev,
image: event.target.files[0]
}));
}