Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React event target value in onChange

What is the difference between

onChange={({ target: { value } }) => setInsertedTitle(value)}

and

onChange={setInsertedTitle}

When should one or another be used?

like image 822
rumon Avatar asked Apr 24 '26 03:04

rumon


1 Answers

Using onChange={({ target: { value } }) => setInsertedTitle(value)} you are passing the current target value as a parameter.

It is because onChange generates an Event, and you access the value by event.target.value ...

event: {
  target: {
    value: "string"
  }
}

On the other hand, when you use the function like in onChange={setInsertedTitle}, it receives the event.

You can see it here: https://codesandbox.io/s/compassionate-fast-krrib?file=/src/App.js

like image 105
Kevin Mamaqi Avatar answered Apr 26 '26 16:04

Kevin Mamaqi