[问题] 有关ReactJS state的问题

楼主: hiphopadd7 (来自北方)   2016-11-20 03:50:13
各位板上的神人好
小弟我最近入坑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却不行呢?
作者: No (you stay there)   2016-11-20 03:59:00
setState 那边的写法,name 会设到 state 的第一层
楼主: hiphopadd7 (来自北方)   2016-11-21 01:16:00
那要怎么写才能让他在第一层跟第二层之间作动态挑选?
作者: gopupkimo (BUG)   2016-11-23 21:12:00
其实你这问题跟state没关系,是如何操作物件的问题如果需求是这样,何不用两个function去分开做?
楼主: hiphopadd7 (来自北方)   2016-11-24 18:35:00
摁,我后来发现分成两个操作会方便很多~刚入坑而已,有多到满出来的问号,哈哈

Links booklink

Contact Us: admin [ a t ] ucptt.com