Is it, and why is it a bad idea to mutate a state from React's new useState hook? I found no info on the topic.
Consider following code:
const [values, setValues] = useState({})
// doSomething can be called once, or multiple times per render
const doSomething = (name, value) => {
values[name] = value
setValues({ ...values })
}
Note the mutation of values. Since doSomething can be called more than once per render, doing this would not work because of the async properties of setState:
const doSomething = (name, value) => {
setValues({ ...values, [name]: value })
}
Is the approach of mutating values directly the correct one in this case?