Lottie Animation View for React
react-lottie is a React component that wraps `bodymovin.js` (now known as Lottie Web) to render animations exported from Adobe After Effects as JSON. The current stable version, 1.2.10, was released in 2018. It allows developers to integrate vector animations into React applications, offering programmatic control over playback (play, pause, stop), and supporting various After Effects features like shape layers, masks, and trim paths. Its key differentiators include enabling interactive animations with small file sizes, and the ability to load animation data from local assets or remote APIs. However, due to its age and lack of maintenance, developers should be aware of potential compatibility issues with modern React versions and ecosystem changes, making newer alternatives a more robust choice for contemporary projects.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'play') at Lottie.componentDidMount
cause The `animationData` prop in `options` is missing, `null`, or undefined when the component tries to initialize the Lottie animation.fixEnsure that your `.json` animation file is correctly imported (e.g., `import * as animationData from './myAnimation.json'`) and passed as `animationData: animationData` within the `options` prop object. -
Warning: componentWillReceiveProps has been renamed, and is not recommended for use.
cause This warning occurs because `react-lottie` internally uses deprecated React lifecycle methods, which are flagged by newer React versions.fixThis is a warning originating from the unmaintained library itself and doesn't directly indicate a problem with your code. While it's often harmless, for a clean console and future compatibility, migrating to a different, actively maintained Lottie player for React is recommended. -
Module not found: Error: Can't resolve 'react-lottie'
cause The `react-lottie` package has not been installed, or there's a bundler configuration issue preventing its resolution (e.g., trying to import CJS in an ESM-only context without proper transpilation).fixFirst, ensure `npm install --save react-lottie` or `yarn add react-lottie` has been run. If the issue persists, check your bundler (Webpack, Rollup, Vite) configuration to ensure it correctly handles CommonJS modules from `node_modules`.
Warnings
- breaking The `react-lottie` package is effectively abandoned and has not been updated since 2018. It relies on an outdated version of `bodymovin.js` (now `lottie-web`) and may have compatibility issues with modern React versions (e.g., React 18+), strict mode, or contemporary build tools.
- gotcha This package uses deprecated React lifecycle methods (e.g., `componentWillReceiveProps`) internally. While this often results only in console warnings in newer React versions, it might lead to unexpected behavior or future breakage.
- gotcha Due to its age, `react-lottie` may have sub-optimal performance characteristics compared to newer implementations, especially concerning animations with many layers or complex interactions.
- gotcha The documentation and underlying library references in `react-lottie` still refer to `bodymovin.js`, which has since been renamed to `lottie-web`. This indicates the library is not aligned with the current Lottie ecosystem.
Install
-
npm install react-lottie -
yarn add react-lottie -
pnpm add react-lottie
Imports
- Lottie
import { Lottie } from 'react-lottie';import Lottie from 'react-lottie';
- animationData (JSON)
import animationData from './path/to/animation.json';
import * as animationData from './path/to/animation.json';
- Lottie (CommonJS)
const Lottie = require('react-lottie');const Lottie = require('react-lottie').default;
Quickstart
import React, { useState } from 'react';
import Lottie from 'react-lottie';
import * as animationData from './pinjump.json'; // Ensure this file exists in your project
// To run this example, first install: npm install --save react-lottie
export default function LottieControl() {
const [isStopped, setIsStopped] = useState(false);
const [isPaused, setIsPaused] = useState(false);
const buttonStyle = {
display: 'block',
margin: '10px auto',
padding: '8px 15px',
fontSize: '16px',
cursor: 'pointer'
};
const defaultOptions = {
loop: true,
autoplay: true,
animationData: animationData, // The imported JSON animation data
rendererSettings: {
preserveAspectRatio: 'xMidYMid slice'
}
};
return (
<div style={{ textAlign: 'center', padding: '20px' }}>
<h1>Lottie Animation Example</h1>
<Lottie
options={defaultOptions}
height={400}
width={400}
isStopped={isStopped}
isPaused={isPaused}
/>
<button style={buttonStyle} onClick={() => setIsStopped(true)}>Stop</button>
<button style={buttonStyle} onClick={() => setIsStopped(false)}>Play</button>
<button style={buttonStyle} onClick={() => setIsPaused(!isPaused)}>Pause/Resume</button>
</div>
);
}