42
loading...
This website collects cookies to deliver better user experience
react-lottie
package, which has immediate compatibility with react and is still intuitive because we pass the appropriate animation information through attributes.lottie-web
package, because this way you can apply the same knowledge in other libraries/frameworks.npm i lottie-web
src/
) create a folder called animations (src/animations
) and drag the JSON file into that folder. Then rename the file to duck.json (src/animations/duck.json
).// @src/components/duck.jsx
import React from "react";
const Duck = () => {
return <h1>Duck Component 🦆</h1>
};
export default Duck;
duck.jsx
we are going to import useEffect()
and useRef()
hooks from React. Then we'll create a reference called anime to directly access the web element we're going to add our animation to.// @src/components/duck.jsx
import React, { useEffect, useRef } from "react";
const Duck = () => {
const anime = useRef(null);
useEffect(() => {
// Logic goes here
}, []);
return <div ref={anime}></div>;
};
export default Duck;
// @src/components/duck.jsx
import React, { useEffect, useRef } from "react";
import lottie from "lottie-web";
import duckAnimation from "../animations/duck.json";
const Duck = () => {
const anime = useRef(null);
useEffect(() => {
// Logic goes here
}, []);
return <div ref={anime}></div>;
};
export default Duck;
// @src/components/duck.jsx
import React, { useEffect, useRef } from "react";
import lottie from "lottie-web";
import duckAnimation from "../animations/duck.json";
const Duck = () => {
const anime = useRef(null);
useEffect(() => {
lottie.loadAnimation({
container: anime.current,
renderer: "svg",
loop: true,
autoplay: true,
animationData: duckAnimation,
});
// More logic goes here
}, []);
return <div ref={anime}></div>;
};
export default Duck;
useEffect()
hook, this is because we want to stop the animation as soon as the component unmounts.// @src/components/duck.jsx
import React, { useEffect, useRef } from "react";
import lottie from "lottie-web";
import duckAnimation from "../animations/duck.json";
const Duck = () => {
const anime = useRef(null);
useEffect(() => {
lottie.loadAnimation({
container: anime.current,
renderer: "svg",
loop: true,
autoplay: true,
animationData: duckAnimation,
});
return () => lottie.stop();
}, []);
return <div ref={anime}></div>;
};
export default Duck;
// @src/components/duck.jsx
import React, { useEffect, useRef } from "react";
import lottie from "lottie-web";
import duckAnimation from "../animations/duck.json";
const Duck = () => {
const anime = useRef(null);
useEffect(() => {
lottie.loadAnimation({
container: anime.current,
renderer: "svg",
loop: true,
autoplay: true,
animationData: duckAnimation,
});
return () => lottie.stop();
}, []);
return <div style={{ height: 250, width: 300 }} ref={anime}></div>;
};
export default Duck;
App.jsx
).// @src/App.jsx
import React from "react";
const App = () => {
return (
<div>
<h1>Lottie animations.</h1>
<p>Lets use it with React.js</p>
<br />
<button>Click me</button>
{/* Animation goes here */}
</div>
);
};
export default App;
useState()
hook so we can control the local state of our component. The only state we're going to control is if the button is clicked. Because we'll want to show or hide our animation accordingly.// @src/App.jsx
import React, { useState } from "react";
const App = () => {
const [isClicked, setIsClicked] = useState(false);
return (
<div>
<h1>Lottie animations.</h1>
<p>Lets use it with React.js</p>
<br />
<button onClick={() => setIsClicked(!isClicked)}>
{isClicked ? "Hide" : "Show"} duck
</button>
{/* Animation goes here */}
</div>
);
};
export default App;
// @src/App.jsx
import React, { useState } from "react";
import Duck from "./components/duck";
const App = () => {
const [isClicked, setIsClicked] = useState(false);
return (
<div>
<h1>Lottie animations.</h1>
<p>Lets use it with React.js</p>
<br />
<button onClick={() => setIsClicked(!isClicked)}>
{isClicked ? "Hide" : "Show"} duck
</button>
{isClicked && <Duck />}
</div>
);
};
export default App;