【发布时间】:2016-12-04 20:05:23
【问题描述】:
我看到一个 onChange 监听器除了e之外通常没有额外的参数。
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>;
}
}
【问题讨论】:
标签: javascript reactjs ecmascript-6