我有嵌套表格對象,但我嘗試使用一個處理程序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