The imperative API
Sometimes, it's just easier using imperative programming in our app, for example, sometimes we need to play/stop our animation based on user interactions inside the app. For these cases, we can use the lottie-react-native
library's imperative API, which allows developers to control the animation playback in a very straightforward way.
React automatically provides the ref
prop on all its components and the useRef
hook to be used whenever we need to control the component in an imperative way.
The piece of code we wrote in the previous section is actually a good example of how imperative programming makes more sense when we need to directly trigger an action on a component, improving readability and making our code easier to reason about:
import React, {useEffect, useRef} from 'react';
import LottieView from 'lottie-react-native';
import {SafeAreaView, View, StyleSheet} from 'react-native';
const numFramesPerFile...