![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
lottie-react
Advanced tools
The lottie-react npm package is a React wrapper for Lottie animations, which allows you to easily integrate and control Lottie animations in your React applications. Lottie is a library for rendering Adobe After Effects animations in real-time, which are exported as JSON files using the Bodymovin plugin.
Basic Animation
This feature allows you to render a basic Lottie animation in your React component. You simply import the Lottie component and the animation JSON file, then use the Lottie component with the animationData prop.
import React from 'react';
import Lottie from 'lottie-react';
import animationData from './path/to/animation.json';
const BasicAnimation = () => {
return <Lottie animationData={animationData} />;
};
export default BasicAnimation;
Controlled Animation
This feature allows you to control the playback of the Lottie animation. In this example, a button is used to toggle the animation between play and pause states using the isPaused prop.
import React, { useState } from 'react';
import Lottie from 'lottie-react';
import animationData from './path/to/animation.json';
const ControlledAnimation = () => {
const [isPaused, setIsPaused] = useState(false);
return (
<div>
<Lottie animationData={animationData} isPaused={isPaused} />
<button onClick={() => setIsPaused(!isPaused)}>
{isPaused ? 'Play' : 'Pause'}
</button>
</div>
);
};
export default ControlledAnimation;
Looping Animation
This feature allows you to set the animation to loop continuously. By setting the loop prop to true, the animation will restart automatically after it finishes.
import React from 'react';
import Lottie from 'lottie-react';
import animationData from './path/to/animation.json';
const LoopingAnimation = () => {
return <Lottie animationData={animationData} loop={true} />;
};
export default LoopingAnimation;
Event Listeners
This feature allows you to add event listeners to the Lottie animation. In this example, the onComplete event listener is used to log a message when the animation completes.
import React from 'react';
import Lottie from 'lottie-react';
import animationData from './path/to/animation.json';
const AnimationWithEvents = () => {
const handleComplete = () => {
console.log('Animation completed');
};
return <Lottie animationData={animationData} onComplete={handleComplete} />;
};
export default AnimationWithEvents;
react-lottie is another React wrapper for Lottie animations. It provides similar functionality to lottie-react, allowing you to render and control Lottie animations in your React applications. However, lottie-react is often preferred for its more modern API and better performance.
lottie-web is the core library for rendering Lottie animations in web applications. While it is not specifically designed for React, it can be used with React by manually integrating it into your components. lottie-react simplifies this process by providing a dedicated React component.
react-lottie-player is another alternative for integrating Lottie animations in React applications. It offers a similar set of features to lottie-react, including support for controlling playback and adding event listeners. Some developers prefer react-lottie-player for its additional customization options.
This project is meant to give developers full control over Lottie instance with minimal implementation by wrapping lottie-web in a Component or Hook that can be easily used in React applications.
Install react, react-dom and prop-types if you don’t have them already:
Note: This library is using React hooks so the minimum version required for both react and react-dom is 16.8.0.
npm i react@^16.8.0 react-dom@^16.8.0 prop-types@^15.5.7
Install lottie-react using npm:
npm i lottie-react
import Lottie from "lottie-react";
import groovyWalkAnimation from "./groovyWalk.json";
const Example = () => {
return <Lottie animationData={groovyWalkAnimation} />;
};
export default Example;
import { useLottie } from "lottie-react";
import groovyWalkAnimation from "./groovyWalk.json";
const Example = () => {
const options = {
animationData: groovyWalkAnimation,
loop: true,
autoplay: true,
};
const { View } = useLottie(options);
return View;
};
export default Example;
Checkout the Documentation for more information and examples.
npm test
-----------------------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-----------------------------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
components | 100 | 100 | 100 | 100 |
Lottie.ts | 100 | 100 | 100 | 100 |
hooks | 100 | 100 | 100 | 100 |
useLottie.tsx | 100 | 100 | 100 | 100 |
useLottieInteractivity.tsx | 100 | 100 | 100 | 100 |
-----------------------------|---------|----------|---------|---------|-------------------
Let us know if you have any suggestions or contributions. This package has the mission to help developers, so if you have any features that you think we should prioritize, reach out to us.
lottie-react is available under the MIT license.
Thanks to David Probst Jr for the animations used in the examples.
FAQs
Lottie for React
The npm package lottie-react receives a total of 682,777 weekly downloads. As such, lottie-react popularity was classified as popular.
We found that lottie-react demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.