0
我試圖更新狀態時,新的員工數據輸入。但推功能沒有插入新的員工數據的狀態。在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: '[email protected]',
phone: '0443322118'
},
{ id: '2',
name: 'Dani',
email: '[email protected]',
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>
);}
}
'使用狀態正確'+1 –