0
我有這樣的代碼在本地做出反應React Native如何將狀態恢復爲默認狀態?
class Home extends Component {
constructor(props) {
super(props);
this.initialState={
backGroundColor: '#ffffff',
visible:[true,true,true,true,true,true],
progress:0,
numbers: shuffle([1,2,3,4,5,6])
}
this.state = this.initialState;
}
_resetState(){
this.setState(this.initialState);
}
_change(teste,op){
if(op==this.state.numbers[0]){
let array = this.state.visible;
switch(op){
case 1:
array[op-1] = false;
break;
case 2:
array[op-1] = false;
break;
case 3:
array[op-1] = false;
break;
case 4:
array[op-1] = false;
break;
case 5:
array[op-1] = false;
break;
case 6:
array[op-1] = false;
break;
}
this.setState({backGroundColor: teste,visible:array,progress: this.state.progress+0.16666666666666});
this.state.numbers.splice(0,1);
}else{
console.log(this.initialState);
}
}
當我做的console.log(this.initialState)的陣列數量和可見裏面的狀態不是初始但BACKGROUNDCOLOR和進步是初始值。爲什麼會發生?我怎樣才能保持他們的初始值?我需要回到初始狀態的所有元素(包括兩個數組)。