I am new to react and would like to ask a question,
I have a userInput object going into a useState function, with variables for my form, such as Name, Price, etc. and Image!
The problem is, the tutorial I was following did not include a Image input, so I am implementing this on my own, but now I am stuck.
So this was the working version:
<input
type="file"
onChange={imageChangeHandler}
/>
userInput is an object:
const [userInput, setUserInput] = useState({
enteredTitle: "",
enteredImage: "",
enteredAuthor: "",
enteredDescription: "",
enteredPrice: "",
enteredQuantity: "",
});
the function passed to handle the file input:
//image URL var
const imageChangeHandler = (event) => {
if (event.target.files && event.target.files[0]) {
setUserInput((prevState) => {
return {
...prevState,
enteredImage: URL.createObjectURL(event.target.files[0]),
};
});
}
};
It was working, but then i have added the value
parameter
<input
type="file"
value={userInput.enteredImage}
onChange={imageChangeHandler}
/>
and now it is no longer working, any help is highly appreciated.