Skip to content
Advertisement

React Native Lottie Animation Only Plays On First Tap

So essentially, I want to play the lottie animation everytime it is tapped. Here is my UI code for the lottie animation:

JavaScript

Here is my state code for the lottie animation:

JavaScript

On the first tap, the animation play perfefctly fine. But on all other taps, the animation won’t play. I tried pausing the animation in the onAnimationFinish and then resuming it, but that also didn’t work. Am I missing something?

EDIT

I got rid of the resetAnimation() in the onAnimationFinish and that solved the initial problem. But the thing is, I want the animation to be reset to the beginning every time. Why does it break when I reset the animation?

Advertisement

Answer

After coming back to this problem a few days later, I found the solution

Playing the lottie animation seems to be considered a side effect, therefore, editing the references to the animations should be done in a useEffect hook

The solution that worked for me:

(again, in this code, I want the animation to reset to the beginning before the user taps the screen screen again.

state code

JavaScript

UI code

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement