Noo*_*obs 6 javascript functional-programming currying reactjs
我试图onChange在Component上写一个(curried?)事件处理程序,它将接收一个key参数,让它知道要更新的状态对象中的哪个键.代码不会编译,说'key' is not defined.
class App extends Component {
constructor(props) {
super(props);
this.state = {
firstName: null,
lastName: null
}
this.handleChange = this.handleChange.bind(this);
}
handleChange = (key) = (event) => {
console.log(key, event);
}
render() {
return (
<div>
<form>
<input onChange={this.handleChange('firstName')} value={this.state.firstName} />
<input onChange={this.handleChange('lastName')} value={this.state.firstName} />
</form>
{JSON.stringify(this.state, null, 4)}
</div>
);
}
}
Run Code Online (Sandbox Code Playgroud)
您必须同时传递event和key处理程序OnChange。
做这个
<input onChange={this.handleChange.bind(this,'firstName')} value={this.state.firstName} />
Run Code Online (Sandbox Code Playgroud)
并且onChange应该是
handleChange = (key, event) => {
console.log(key, event);
}
Run Code Online (Sandbox Code Playgroud)
这将允许传递event和 ,并且该函数将起作用。key
| 归档时间: |
|
| 查看次数: |
1558 次 |
| 最近记录: |