Back to Lottie React Native

Api

docs/api.md

7.3.613.1 KB
Original Source

Component API

PropDescriptionDefaultPlatform
sourceMandatory - The source of animation. Can be referenced as a local asset by a string, or remotely with an object with a uri property, or it can be an actual JS object of an animation, obtained (for example) with something like require('../path/to/animation.json')NoneAll
progressA number between 0 and 1. This number represents the normalized progress of the animation. If you update this prop, the animation will correspondingly update to the frame at that progress value. This prop is not required if you are using the imperative API.0iOS, Android, Windows
speedThe speed the animation will progress. Sending a negative value will reverse the animation1All
durationThe duration of the animation in ms. Takes precedence over speed when set. This only works when source is an actual JS object of an animation.undefinediOS, Android, Windows
loopA boolean flag indicating whether or not the animation should loop.trueAll
autoPlayA boolean flag indicating whether or not the animation should start automatically when mounted. This only affects the imperative API.falseAll
resizeModeDetermines how to resize the animated view when the frame doesn't match the raw image dimensions. Supports cover, contain and center.containiOS, Android, Windows
styleStyle attributes for the view, as expected in a standard View, aside from border stylingNoneiOS, Android, Windows
containerStyleStyle attributes for the outermost container view, as expected in a standard View. Useful for layout and positioning.NoneiOS, Android, VisionOS
webStyleStyle attributes for the view, it uses CSSProperties.NoneWeb
imageAssetsFolderNeeded for Android to work properly with assets, iOS will ignore it.NoneAndroid
useNativeLoopingOnly Windows. When enabled, uses platform-level looping to improve smoothness, but onAnimationLoop will not fire and changing the loop prop will reset playback rather than finishing gracefully.falseWindows
onAnimationLoopOnly Windows and Web. A callback function invoked when the animation loops.NoneWindows, Web
onAnimationLoadedA callback function which will be called when animation is done loading. This callback is called with no parameters.NoneAll
onAnimationFailureA callback function which will be called if an error occurs while working with the animation (loading, running, etc). This callback is called with a string error argument, which contains the error message that occured.NoneAll
onAnimationFinishA callback function which will be called when animation is finished. This callback is called with a boolean isCancelled argument, indicating if the animation actually completed playing, or if it was cancelled, for instance by calling play() or reset() while is was still playing. Note that this callback will be called only when loop is set to false.NoneAll
renderModea String flag to set whether or not to render with HARDWARE or SOFTWARE accelerationAUTOMATICiOS, Android
cacheCompositionOnly Android, a boolean flag indicating whether or not the animation should do caching.trueAndroid
colorFiltersAn array of objects denoting layers by KeyPath and a new color filter value (as hex string).[]iOS, Android, Windows
textFiltersAndroidOnly Android, an array of objects denoting text values to find and replace.[]Android
textFiltersIOSOnly iOS, an array of objects denoting text layers by KeyPath and a new string value.[]iOS
hoverOnly Web, a boolean denoting whether to play on mouse hover.falseWeb
directionOnly Web a number from 1 | -1 denoting playing direction.1Web

Methods (Imperative API):

MethodDescription
playPlay the animation all the way through, at the speed specified as a prop. It can also play a section of the animation (not available on web) when called as play(startFrame, endFrame).
resetReset the animation back to 0 progress.
pausePauses the animation.
resumeResumes the paused animation.

Using animations with assets

When creating animations using AfterEffects and bodymovin, the exported json may have some assets to rely on, specified like this:

json
  ...,
  "assets": [
    {
      "id": "image_0",
      "w": 737,
      "h": 1215,
      "u": "images/",
      "p": "img_0.png"
    }
  ],
  ...

To make react-native-lottie use those assets properly, it is necessary to go for the native route: so remember that you need to fully rebuild your application if you modify the images / add new ones.

Android

You need to copy your images into [PROJECT FOLDER]/android/app/src/main/assets. It is suggested to create a lottie subfolder, and eventually a folder per animation. You will then need to refer that folder, via its relative path, in the imageAssetsFolder prop for the animation - ex: imageAssetsFolder={'lottie/animation_1'}.

iOS

You need to open XCode, right click on the Resources folder (on the left column), "Add file to [Project]" and select the images required.

Renaming assets

If you find yourself in the necessity to rename the images (ex. multiple animations with assets), you can - but you need to remember to modify the name in the json file too, like:

json
  ...,
  "assets": [
    {
      "id": "image_0",
      "w": 737,
      "h": 1215,
      "u": "images/",
      "p": "new_snihy_name.png"  <---- HERE!
    }
  ],
  ...