I am trying to partially change the state of my component, but unfortunately it isn't performing as I'm excpecting.
I initialize the state in this way :
constructor(props){
super(props);
this.state = {
array: [],
status: false,
interval: null,
animations: [],
algorithm: null
}
}
The way I try to use it
if(this.state.status === false){
this.change_style(text);
//The change
this.setState({status: true, animations: [], algorithm: "Bubble_Sort"});
const arr = this.state.array.slice();
this.bubble_sort_and_push(this.state.animations, arr);
this.animate(this.state.animations);
}
After some research I saw that setState
only changes the state if there is an actual change between the prev state to the new one (In my case it does have differences)
So I can't really grasp why the changes don't go through.
I tried changing only one of the fields
if(this.state.status === false){
this.change_style(text);
console.log(this.state.status);
//The change
this.setState({status: true})
console.log(this.state.status);
const arr = this.state.array.slice();
this.bubble_sort_and_push(this.state.animations, arr);
this.animate(this.state.animations);
}
There was no difference between the console.log
calls, but the change did happen.
How can I make the setState
perform the moment I invoke it?
I have tried using forceUpdate()
and read that it has connection to DidComponentUpdate
, but can't really figure out how to connect them to work like I expect.