看板 Ajax 關於我們 聯絡資訊
各位板上的神人好 小弟我最近入坑React 遇到一些問題 關於這段程式 class MyApp extends React.Component{ constructor(props){ super(props); this.state = { employee:{ name: 'hello', birthday: '' }, price: '' } this.handleValueChange = this.handleValueChange.bind(this); } handleValueChange(event){ valueName = event.target.name this.setState({[valueName]: event.target.value}); }; render(){ return( <form> <input type="text" placeholder="name" value={this.state.employee.name} onChange={this.handleValueChange} name="name"/> <input type="text" placeholder="name" value={this.state.price} onChange={this.handleValueChange} name="price"/> </form> ) } } 為什麼我的handleValueChange再更改this.state.price是可以的 可是更改employee.name卻不行呢? -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 115.43.246.35 ※ 文章網址: https://www.ptt.cc/bbs/Ajax/M.1479585016.A.C8B.html
No: setState 那邊的寫法,name 會設到 state 的第一層 11/20 03:59
hiphopadd7: 那要怎麼寫才能讓他在第一層跟第二層之間作動態挑選? 11/21 01:16
gopupkimo: 其實你這問題跟state沒關係,是如何操作物件的問題 11/23 21:12
gopupkimo: 如果需求是這樣,何不用兩個function去分開做? 11/23 21:14
hiphopadd7: 摁,我後來發現分成兩個操作會方便很多 11/24 18:35
hiphopadd7: ~剛入坑而已,有多到滿出來的問號,哈哈 11/24 18:36