我在模態中渲染數組。一旦模式關閉,我需要清空數組。下面的代碼更新數組,但點擊closeModal時不清除數組。如何在反應0.14 ES6上點擊清除狀態或清空陣列?
constructor(props,context) {
super(props,context);
this.state = {
myArray: []
};
}
pushData(newVar) {
this.setState((state) => {
myArray: state.myArray.push(newVar)
});
}
closeModal() {
this.setState({
myArray: []
})
}
喜歡的東西this.state.myarray = [] – Antonio
嘿對不起,你是對的@Antonio this.state.myarray = []固定我的問題。 –
推數據似乎很奇怪...我會寫this.state.myArray.push(newVar) – Antonio