I wonder why this.setState function is giving this error when I START TYPING. I am using the arrow function for the same. It gives me the error 'Cannot read property 'name' of null'. But it works when i use the simple direct way of changing the state.
Here is my code:
class UpstreamForm extends Component {
state = {
name: '',
email: ''
}
onSubmitHandler() {
this.props.changeData(this.state)
}
changeHander(event) {
this.setState((state) => {
return {
[event.target.name] : event.target.value
}
})
}
render() {
return (
<div>
<form onSubmit={this.onSubmitHandler.bind(this)}>
<input
type="text"
placeholder="name"
name="name"
onChange={this.changeHander.bind(this)}
/>
<input
type="text"
placeholder="email"
name="email"
onChange={this.changeHander.bind(this)}
/>
<button>Submit!</button>
</form>
</div>
)
}
}
The one below works, but why not the one I did above?? REALLY CURIOUS!
changeHandler = (event) =>
this.setState({
[event.target.name] : event.target.value
})