Guide · Lottie in React
How to use a Lottie animation in React
A short, working guide to lottie-react version 3: install it, render an animation, control playback, and play it when it scrolls into view. It works in Vite, Create React App and the Next.js App Router. Don’t have a Lottie file yet? Make one from any SVG in Reframe, free.
The four steps
From an SVG to an animation playing in your React app.
- 1
Get a Lottie JSON file
Export one from Reframe (upload an SVG, animate it with an AI prompt or a preset, export Lottie JSON), or use a file from your designer.
- 2
Install lottie-react
Run npm i lottie-react. It needs React 18.2 or newer and brings lottie-web with it.
- 3
Put the file in your public folder
Save it as public/animations/logo.json so it’s served as a static file and stays out of your JavaScript bundle.
- 4
Render the component
Use <Lottie src="/animations/logo.json" autoplay loop /> and give it a width and height. The animation fills the element.
The code
Install
One package. lottie-web, the engine that draws the animation, is installed with it.
npm i lottie-reactRender an animation
Import the named Lottie component and point src at the file. The component fills its element, so size the element with a style or a class.
import { Lottie } from "lottie-react";
export function Logo() {
return (
<Lottie
src="/animations/logo.json"
autoplay
loop
style={{ width: 240, height: 240 }}
/>
);
}src takes a URL or path, or the parsed animation object. loop also takes a number, for example loop={2} to play twice and stop.
Coming from lottie-react v2?
Version 3 changed the API, and most tutorials and AI answers still show the old one. There is no default export any more, and animationData is now src.
// v2 (no longer works)
import Lottie from "lottie-react";
<Lottie animationData={logo} loop />
// v3
import { Lottie } from "lottie-react";
<Lottie src={logo} autoplay loop />The library’s migration guide at lottiereact.com maps every v2 prop and method to its v3 equivalent.
Control playback
Pass a ref through lottieRef to get play, pause, stop, seek and speed controls.
import { useRef } from "react";
import { Lottie, type LottieHandle } from "lottie-react";
export function ControlledLogo() {
const lottie = useRef<LottieHandle>(null);
return (
<div>
<Lottie
src="/animations/logo.json"
autoplay={false}
lottieRef={lottie}
style={{ width: 200, height: 200 }}
/>
<button onClick={() => lottie.current?.play()}>Play</button>
<button onClick={() => lottie.current?.pause()}>Pause</button>
<button onClick={() => lottie.current?.setSpeed(2)}>2× speed</button>
<button onClick={() => lottie.current?.seek({ percent: 0 })}>Restart</button>
</div>
);
}seek takes a frame number, or { seconds }, { percent } or { marker }.
Play it when it scrolls into view
Wrap the animation in LottieInteractions with lottieInView. It plays when the animation enters the viewport and pauses when it leaves. With once: true it plays on the first entry only, so scrolling past again doesn’t replay it.
import { Lottie, LottieInteractions, lottieInView } from "lottie-react";
export function RevealOnScroll() {
return (
<LottieInteractions interactions={[lottieInView({ once: true })]}>
<Lottie
src="/animations/logo.json"
autoplay={false}
style={{ width: 200, height: 200 }}
/>
</LottieInteractions>
);
}React to events with the hook
useLottie gives you the same controls plus live state, and lets you subscribe to events such as complete. Attach setDisplayRef to the element the animation should draw into.
import { useLottie } from "lottie-react";
export function OnboardingStep({ onDone }: { onDone: () => void }) {
const lottie = useLottie({
src: "/animations/logo.json",
autoplay: true,
loop: false,
subscriptions: { complete: onDone },
});
return <div ref={lottie.setDisplayRef} style={{ height: 300 }} />;
}Respect reduced motion
Some people turn off animation in their system settings. Check prefers-reduced-motion and don’t autoplay for them.
import { useSyncExternalStore } from "react";
import { Lottie } from "lottie-react";
const query = "(prefers-reduced-motion: reduce)";
const subscribe = (cb: () => void) => {
const mq = window.matchMedia(query);
mq.addEventListener("change", cb);
return () => mq.removeEventListener("change", cb);
};
const getSnapshot = () => window.matchMedia(query).matches;
export function Logo() {
const reduced = useSyncExternalStore(subscribe, getSnapshot, () => false);
return (
<Lottie
src="/animations/logo.json"
autoplay={!reduced}
loop={!reduced}
style={{ width: 240, height: 240 }}
/>
);
}Next.js, bundle size and file loading
In the Next.js App Router you can use <Lottie> straight from a Server Component page. The package marks its components as client components, so the server renders a sized, empty container and the animation starts in the browser once the page hydrates. You don’t need next/dynamic or ssr: false. Wrap it in your own "use client" component only if you’re using the hook or a ref.
Loading the file by URL (src="/animations/logo.json") keeps the animation out of your JavaScript bundle and lets the browser cache it. Importing the JSON (import logo from "./logo.json") bundles it, which is fine for small files you want on screen immediately. If you only need the SVG renderer, which is the default, use LottieSvg instead of Lottie: it takes the same props and loads a smaller build of lottie-web. Files exported from Reframe are plain shape layers, so they play with it.
lottie-react plays Lottie JSON files. The .lottie format is a zipped variant played by LottieFiles’ own @lottiefiles/dotlottie-react. Reframe exports plain Lottie JSON (version 5.7.4, shape layers), so lottie-react plays it as is.
Frequently asked questions
How do I use a Lottie animation in React?
Install lottie-react (npm i lottie-react), put the Lottie JSON file in your public folder, then render <Lottie src="/animations/logo.json" autoplay loop /> with a width and height. In lottie-react 3, Lottie is a named import.
Why does import Lottie from "lottie-react" no longer work?
lottie-react 3 removed the default export and renamed the animationData prop to src. Use import { Lottie } from "lottie-react" and <Lottie src={animation} />. Code written for version 2 needs those two changes.
Does lottie-react work with the Next.js App Router?
Yes. Its components are marked as client components, so you can render <Lottie> from a Server Component page without next/dynamic. The hook and refs need a "use client" component of your own.
How do I play, pause or restart a Lottie animation in React?
Pass a ref with lottieRef={ref}, then call ref.current.play(), pause(), stop(), setSpeed(2) or seek({ percent: 0 }). The useLottie hook returns the same controls along with the current state.
Should I import the Lottie JSON or load it from a URL?
Load it from a URL in your public folder to keep it out of your JavaScript bundle and let the browser cache it. Import it when the file is small and must appear without a network request.
How do I make a Lottie file without After Effects?
Upload an SVG to Reframe, animate it with a plain-English prompt or a preset, and export Lottie JSON. It’s free, needs no account, and Lottie exports are never watermarked.
What’s the difference between lottie-react and dotlottie-react?
lottie-react plays Lottie JSON files using lottie-web. @lottiefiles/dotlottie-react plays the zipped .lottie format with LottieFiles’ own player. Use the one that matches your file type.
Need a Lottie file for your app?
Turn any SVG into Lottie JSON in your browser. Free, no account, never watermarked.
Make a Lottie file from an SVG →