我发现onChange监听器通常除了e
参数之外没有其他额外的参数。
handleOnChange(e) {
this.setState({email: e.target.value});
}
但是是否仍然可以传递额外的参数?像这样:
handleOnChange(e,key) {
this.setState({[key]: e.target.value});
}
我修改了这个帖子中的代码,以便制作一个示例。
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>;
}
}