【发布时间】:2017-09-05 07:43:11
【问题描述】:
我尝试在新员工数据输入时更新状态。但是推送功能没有将新员工数据插入状态。在 addpar 函数中我设置了 console.log 并显示数据存在但它未能将其推向状态
// this class will hold the table and the form
class EmpContainer extends React.Component{
constructor(props) {
super(props);
// the state will have the following data by default
this.state = {participants : [
{ id: '1',
name: 'Dani',
email: 'dani@hotmail.com',
phone: '0443322118'
},
{ id: '2',
name: 'Dani',
email: 'dani@hotmail.com',
phone: '0443322118'
}
]};
}
// this supposed to add the new employed data to the state
addPar (emp){
console.log(emp); // this shows the new employee data
this.state.participants.push(emp);
this.setState({
participants: this.state.participants
});}
render() {
return (
<div>
<AddNewParticipant addNew={this.addPar}/>
</div>
);}
}
【问题讨论】:
标签: javascript reactjs