React Lottie Player
React Lottie Player is a component library for integrating Lottie animations into React applications with a fully declarative API. It wraps the core `lottie-web` library, aiming to provide a more robust experience by correctly handling prop changes for playback control and claiming to prevent memory leaks often associated with `lottie-web` repeaters. The current stable version, `2.1.0`, was published approximately two years ago, suggesting a mature and stable but not rapidly updated codebase for this specific package. Key differentiators include its declarative nature, seamless control of animation states (play, pause, loop, segments) via props, and the provision of a `LottiePlayerLight` variant that avoids the use of `eval` for environments with strict Content Security Policies.
Common errors
-
ReferenceError: document is not defined
cause Attempting to render `react-lottie-player` (or the underlying `lottie-web`) in a Server-Side Rendering (SSR) environment.fixWrap the `Lottie` component with a dynamic import that disables SSR. In Next.js, this is `const DynamicLottie = dynamic(() => import('react-lottie-player'), { ssr: false });`. -
Animation not showing / nothing renders, no errors in console
cause Commonly due to an incorrect path or malformed `animationData` object, or an incorrect `loop`/`play` prop value.fixDouble-check the `animationData` prop. Ensure it's a valid Lottie JSON object, correctly imported, and that `play` is set to `true`. Verify the `style` prop allows the component to be visible (e.g., has `width` and `height`). -
TypeScript error related to `lottie-web` types (e.g., 'AnimationConfig' is missing properties)
cause Mismatched `lottie-web` version types or breaking changes in `lottie-web` that affect `react-lottie-player`'s internal typings.fixEnsure `react-lottie-player` is compatible with your installed `lottie-web` version (if manually installed). Review `react-lottie-player`'s `package.json` for its `lottie-web` peer dependency. If issues persist, consider explicitly installing a compatible version of `@types/lottie-web` or `lottie-web` itself, or reporting an issue to the `react-lottie-player` maintainers.
Warnings
- breaking Version 2.0.0 introduced 'typescript improvements' that could be breaking for existing TypeScript setups. Always review your type definitions and usage when upgrading from v1.x to v2.x.
- gotcha The default `react-lottie-player` bundle uses `eval`, which can violate strict Content Security Policies (CSPs).
- gotcha When using `react-lottie-player` in Server-Side Rendering (SSR) environments like Next.js, you may encounter `ReferenceError: document is not defined` as `lottie-web` (the underlying library) relies on browser APIs.
- gotcha While `react-lottie-player` claims to address memory leaks from `lottie-web` when using repeaters, complex animations or frequent mounting/unmounting of the player can still be resource-intensive.
Install
-
npm install react-lottie-player -
yarn add react-lottie-player -
pnpm add react-lottie-player
Imports
- Lottie
import { Lottie } from 'react-lottie-player'import Lottie from 'react-lottie-player'
- LottiePlayerLight
import { LottiePlayerLight } from 'react-lottie-player'import Lottie from 'react-lottie-player/dist/LottiePlayerLight'
- LottieRefCurrent
const lottieRef = useRef(); // Lacks type inference
const lottieRef = useRef<LottieRefCurrent | null>(null);
Quickstart
import React, { useEffect, useState, useRef } from 'react';
import Lottie from 'react-lottie-player';
// Alternatively for environments with strict CSPs or SSR, use the 'Light' version:
// import Lottie from 'react-lottie-player/dist/LottiePlayerLight';
interface LottieAnimationData {
v: string;
fr: number;
ip: number;
op: number;
w: number;
h: number;
nm: string;
assets: any[];
layers: any[];
}
const LottiePlayerExample: React.FC = () => {
const [animationData, setAnimationData] = useState<LottieAnimationData | null>(null);
const lottieRef = useRef<any>(null); // Type 'any' for simplicity; typically more specific lottie-web instance type
useEffect(() => {
// Dynamically import animation data, useful for code splitting large JSON files
import('./my-lottie-animation.json')
.then(module => setAnimationData(module.default as LottieAnimationData))
.catch(error => console.error('Failed to load Lottie animation:', error));
}, []);
if (!animationData) {
return <div>Loading Lottie animation...</div>;
}
return (
<div>
<h1>My Lottie Animation</h1>
<Lottie
loop
play
animationData={animationData}
lottieRef={lottieRef}
style={{ width: 300, height: 300, border: '1px solid #eee' }}
onEvent={(event: string) => {
// console.log(`Lottie Event: ${event}`);
if (event === 'complete') {
console.log('Animation completed!');
}
}}
/>
<button onClick={() => lottieRef.current?.play()}>Play</button>
<button onClick={() => lottieRef.current?.pause()}>Pause</button>
<button onClick={() => lottieRef.current?.stop()}>Stop</button>
<button onClick={() => lottieRef.current?.setSpeed(lottieRef.current?.getSpeed() * 1.5)}>Speed Up</button>
</div>
);
};
export default LottiePlayerExample;