for some reason, I\'m getting the error \" React- Cannot read property \'setState\' of undefined\". So this.state is never getting updated with the values that the user inputs.
In these event handlers, e.target
is the which triggered the event.
onChangeName(e) {
//this.state.name = e.target.name
this.setState({ name: e.target.name});
}
onChangePassword(e) {
//this.state.password = e.target.name
this.setState({ password: e.target.password });
}
You get an input's value by using its value
property:
onChangeName(e) {
this.setState({name: e.target.value});
}
onChangePassword(e) {
this.setState({password: e.target.value});
}
You also need to ensure this
is bound properly, as per the commented-out code in your constructor.
If you give your inputs suitable name
props, you replace these with a single onChange
handler:
constructor(props) {
super(props)
// ...
this.onChange = this.onChange.bind(this)
}
onChange(e) {
this.setState([e.target.name]: e.target.value})
}
render() {
// ...
// ...
}