React-native-animatable: How to start animation on state change?

Created on 18 Feb 2017  路  5Comments  路  Source: oblador/react-native-animatable

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.

Most helpful comment

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

All 5 comments

Add ref={ci => this.animatedTextRef = ci} to your
Add this to componentDidUpdate()

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

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

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

@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

@ttdat89 Thank you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

iamhaaamed picture iamhaaamed  路  3Comments

ghost picture ghost  路  3Comments

sundayhd picture sundayhd  路  5Comments

quangtruong16994 picture quangtruong16994  路  5Comments

TrustDec picture TrustDec  路  3Comments