Install react-lottie-player via npm
masterTo use the declarative React Lottie player in your project, install it using npm:
npm install --save react-lottie-playerrepository·master·Indexed 19 days ago
https://github.com/mifi/react-lottie-playerA fully declarative React wrapper for lottie-web (version 2.1.0) that simplifies Lottie animation control. It handles prop changes for playback, speed, and segments while providing a hooks-based implementation to prevent memory leaks. The library supports loading animations via JSON animationData or URL paths, offers a LottiePlayerLight version to avoid using eval, and provides an imperative API via refs to access the underlying AnimationItem instance.
To use the declarative React Lottie player in your project, install it using npm:
npm install --save react-lottie-playerTo test the react-lottie-player package using the provided example project, navigate to the example directory and use yarn to install dependencies and start the development server.
yarn install
yarn startTo optimize performance, you can lazy load animations using one of three methods:
React.lazy)Wrap the Lottie component in a separate file and use React.lazy to import it.
Use useEffect to dynamically import the JSON data and store it in local state.
path URLPass a direct URL to the path prop to let the player fetch the JSON automatically.
// Option 1: React.lazy
import React from 'react';
const MyLottieAnimation = React.lazy(() => import('./MyLottieAnimation'));
export default function MyComponent() {
return <MyLottieAnimation play />;
}
// Option 2: dynamic import with state
const Example = () => {
const [animationData, setAnimationData] = useState<object>();
useEffect(() => {
import('./animation.json').then(setAnimationData);
}, []);
if (!animationData) return <div>Loading...</div>;
return <Lottie animationData={animationData} />;
}
// Option 3: path URL
const Example = () => <Lottie path="https://example.com/lottie.json" />;The default Lottie component uses eval for certain operations. If your environment or security policy prohibits eval, import the light version instead:
import Lottie from 'react-lottie-player/dist/LottiePlayerLight'
The Lottie component allows you to render animations using either animationData (a JSON object) or a path (a URL). It is fully declarative, meaning you can control playback via props like play and loop.
import React from 'react'
import Lottie from 'react-lottie-player'
import lottieJson from './my-lottie.json'
export default function Example() {
return (
<Lottie
loop
animationData={lottieJson}
play
style={{ width: 150, height: 150 }}
/>
)
}To make the animation fill its entire container (similar to CSS object-fit: cover), pass preserveAspectRatio: 'xMidYMid slice' via the rendererSettings prop.
<Lottie rendererSettings={{ preserveAspectRatio: 'xMidYMid slice' }} />While the component is primarily declarative, you can access the underlying player instance using a ref. This allows you to access properties like currentFrame directly.
const lottieRef = useRef();
useEffect(() => {
console.log(lottieRef.current.currentFrame);
}, [])
return <Lottie ref={lottieRef} />;The Lottie component accepts LottieProps, which combines standard HTML div attributes with Lottie-specific configuration. You can load animations using either a path (URL string) or animationData (JSON object).
<Lottie
path="/animation.json"
// OR
animationData={animationData}
play={true}
loop={true}
/>The following props are available for controlling the Lottie player:
play: boolean - Controls whether the animation is playing.speed: number - The playback speed.direction: AnimationDirection - The direction of the animation.loop: boolean - Whether the animation should loop.goTo: number - The frame to jump to.segments: AnimationSegment | AnimationSegment[] - Specific segments of the animation to play.useSubframes: boolean - Whether to use subframes for smoother animation.renderer: RendererType - The type of renderer to use (e.g., 'svg', 'canvas', 'html').rendererSettings: object - Settings specific to the chosen renderer.audioFactory: any - Factory for audio.onLoad: AnimationEventCallback - Triggered when the animation is loaded.onComplete: AnimationEventCallback - Triggered when the animation finishes playing.onLoopComplete: AnimationEventCallback - Triggered when a loop completes.onEnterFrame: AnimationEventCallback - Triggered on every frame.onSegmentStart: AnimationEventCallback - Triggered when a segment starts.You can access the underlying Lottie AnimationItem instance by providing a ref to the Lottie component. This allows you to call imperative methods provided by the lottie-web library.
import { useRef } from 'react';
import Lottie, { AnimationItem } from 'react-lottie-player';
const MyComponent = () => {
const lottieRef = useRef<AnimationItem | undefined>(null);
const handlePlay = () => {
lottieRef.current?.play();
};
return <Lottie ref={lottieRef} path="/anim.json" />;
};