I'm trying to update a nested object in React using setState
, which is in a function that is called from a form onChange
event. The code below works the first time it is called, but after that I get an error Cannot set properties of undefined (setting 'value')
. What am I doing wrong?
const initialState = {
email: { value: "", hasError: true, error: "" },
password: { value: "", hasError: true, error: "" }
}
const [state, setState] = useState(initialState);
function handleChange(event) {
setState(state[event.target.name].value = event.target.value);
}