S
S
Swaer2020-02-07 13:01:51
React
Swaer, 2020-02-07 13:01:51

How to correctly update an object with values ​​from different inputs?

const [user, setUser] = useState({})
const reg = (e) => {
  const {name, value} = e.target
  setUser(user[name] = value)
}

<input
  name='name'
  onChange={reg}
  value={user.name} />
<input 
        name='surname'
  onChange={reg}
  value={user.surname} />

How to correctly update an object with values ​​from different inputs?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2020-02-07
@Swaer

setUser({ ...user, [name]: value })

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question