2

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.

Sebastian Richner
  • 782
  • 1
  • 13
  • 21
ati
  • 45
  • 2
  • You simply cannot set the `value` property in a file-type input. The value can only come from a user interaction – Phil Nov 17 '22 at 05:26
  • If you want to show an existing value, show it via an `` element... `{userInput.enteredImage && }` – Phil Nov 17 '22 at 05:28

0 Answers0