Consecutive setState calls not working properly

I am creating a component with an animation that occurs when the css class is switched. An example sandbox is here .

The css class is applied conditionally to the field transitioned, so we should get the animation when the field transtionedtakes the form false- true.

Problem:

Animation does not occur if the state, if it is changed as follows:

animateWithoutST = () => {
    this.setState({transitioned: false}, 
    () => this.setState({transitioned: true}))
  }

But it works if the second setStateis called in the callback setTimeoutas follows:

 animateWithST = () => {
    this.setState({ transitioned: false },
    () => {
      setTimeout(() => this.setState({ transitioned: true }))
    }
    )
  }

Why animateWithoutSTdoes it work as expected, although my component is rendering in the correct order?

+6
source share
3

, , , .

, , setTimeout , componentDidUpdate, : , , , - , : " , " .

setTimeout, , . - 0, , > 16 aprox, ( , ).

requestAnimationFrame, , , , .

, , . ?

+4

componentDidUpdate:

componentDidUpdate(prevProps, prevState) {
    if(!this.state.transitioned) {
        this.setState({
            transitioned: true
        });
    }
}

, setState .

0

setState , , . , setstate , . , setstate()

-2

All Articles