react-native-animatable icon indicating copy to clipboard operation
react-native-animatable copied to clipboard

How to start animation on state change?

Open hafiz703 opened this issue 9 years ago • 5 comments

As stated above, how do i invoke an animation when my state changes? Currently the component is being animated at componentDidMount but I would like to trigger the same animation when its state changes.

hafiz703 avatar Feb 18 '17 10:02 hafiz703

Add ref={ci => this.animatedTextRef = ci} to your <Animatable.Text /> Add this to componentDidUpdate()

if(this.animatedTextRef)
   this.animatedTextRef.startAnimation(500, 0, () => {
})

ttdatt avatar Feb 24 '17 06:02 ttdatt

Easiest is to do something like this <Animatable.View animation={this.state.someCondition ? 'fadeIn' : undefined} />.

oblador avatar Mar 19 '17 00:03 oblador

@ttdat89 That snippet worked great! Can you explain what it's doing and how it works?

sskhandek avatar Apr 10 '17 04:04 sskhandek

@sskhandek I take a look on source code https://github.com/oblador/react-native-animatable/blob/master/createAnimatableComponent.js#L239 and saw that Animatable component call startAnimation function in componentDidMount.

So all I did is check if componentDidUpdate is called (state changed), we trigger startAnimation for <Animatable.Text /> element

About what is ref, you could read here https://facebook.github.io/react/docs/refs-and-the-dom.html

ttdatt avatar Apr 10 '17 05:04 ttdatt

@ttdat89 Thank you!

sskhandek avatar Apr 10 '17 18:04 sskhandek