so I'm using the functional form of this.setState() where you pass a function that returns an object.
But when I try to bind the onChange on an input and do this ...
onInputChange = event => {
this.setState(() => ({
...this.state,
[event.target.name]: event.target.value
}));
};
It throws an error saying that Cannot read property 'name' of null obviously the problem is in the event object, so how can I pass the event object to this.setState() ?
I tried this but it didn't work ...
onInputChange = event => {
this.setState((prevState, event) => ({
...this.state,
[event.target.name]: event.target.value,
}));
};
However the only thing that worked for me was doing it like this ...
onInputChange = e => {
const { name, value } = e.target;
this.setState(() => ({
...this.state,
[name]: value
}));
};
But I doubt that this is the right way to dot it.
event.persist()otherwise it will be full of null values -> reactjs.org/docs/events.html...this.stateis not required, also new value is independent of prevState value sofunctional setStateis not required here, you can directly use:this.setState({ [event.target.name]: event.target.value })