我看到一个onChange监听器通常不会有除e
以外的额外参数。传递额外的参数给onChange监听器在reactjs
handleOnChange(e) {
this.setState({email: e.target.value});
}
但是仍然可以传递额外的参数吗?就像这样:
handleOnChange(e,key) {
this.setState({[key]: e.target.value});
}
我修改了代码从this thread做出了榜样
class FormInput extends React.Component{
consturctor(props){
super(props);
this.state = {email:false,password:false}
}
handleOnChange(e,key) {
this.setState({[key]: e.target.value});
}
render() {
return
<form>
<input type="text" name="email" placeholder="Email" onChange={this.handleOnChange('email')} />
<input type="password" name="password" placeholder="Password" onChange={this.handleOnChange('password')}/>
<button type="button" onClick={this.handleLogin}>Zogin</button>
</form>;
}
}
美妙的解决方案。谢谢。 – Shwe