D
D
DeniSidorenko2020-11-05 09:06:03
React
DeniSidorenko, 2020-11-05 09:06:03

React, how to properly load images?

Hello, I can't figure out how to solve this problem.
There is such a state

const [form, setForm] = useState({
    name: "", price: "", weight: "", productImage: "", category: ""
  })

On the onChangeHandler event inputs
const onChangeHandler = event => {
    setForm({...form, [event.target.name] : event.target.value})
  }


But what if I need to send file.
I added this check
const onChangeHandler = event => {
    if(event.target.files){
      const file = event.target.files[0]
      console.log(file)
    }
    setForm({...form, [event.target.name] : event.target.value})
  }


And it really works BUT, I know that a file in this format cannot be put in setForm. You can sort of send it via new FormData()
BUT then other values ​​cannot be sent (whatever on the express server side) get it from req.files.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question