我有嵌套表格对象,但我尝试使用一个处理程序SETSTATE,不知何故不工作计算样式反应不起作用
constructor() {
super()
this.state = {
form: {
name: '',
location: ''
}
}
}
handleFormInput = (event) => {
this.setState({
[this.state.form[event.target.name]]: event.target.value
})
setTimeout(() => {
console.log(this.state.form)
},50)
}
event.target.name
可以的名称和位置。
能否请你澄清你的代码,因为旁边的'构造函数()' 'handleFormInput = ...'是无效的语法,如果是'handleFormInput:事件=> ...'我说的方法检查'this'箭头函数没有自己的'this'对象,他们正在封闭周围一个。或者确切地说'handleFormInput = event => ...'定义了什么?上下文在这里很重要。 – Thomas
@Thomas为什么不呢?它反应es6。 handleFormInput是我jsx中的onChange监听器。 –
这就是我说的是上下文的一部分。所以'handleFormInput = ...'是'渲染()' - 函数里面?通过您的片段看起来就像告诉'handleFormInput'就在你的类中定义的,旁边的'constructor'的方法。 – Thomas