Animation in React: React-Spring

Animation in React: React-Spring

If you’re looking to create animations in React, React Spring is one of the most popular animation libraries to create powerful animation easily.
It can be used to animate HTML, SVG, Native Elements, Three.js, and more.

Introduction React Spring is an advanced yet easy-to-use library for animations and transitions in React applications. It provides a simple and powerful way to create fluid animations using a declarative approach. The library has a well-documented API and offers a range of features, including physics-based animations, interpolation, and chaining.

Interpolation: Basically, interpolation is how we are going to get from one value to another value. Here, It is used to define the values of an animation between the starting and ending points. The library allows you to define the starting and ending values of an animation and the interpolation function to use, making it easy to create smooth and natural-looking animations.

Chaining refers to linking multiple animations together so that they play sequentially allowing you to create complex animations by breaking them down into smaller, more manageable pieces.

Why choose React-Spring?

  1. Physics-based animation: Many libraries that serve animation in React rely on CSS changes like transition, ease-in curve, and duration. Unlike them, it treats animation as general physical property like acceleration, velocity, and spring dynamics allowing you to create smooth, high-performing animation.
  2. Ease of use: It gives us the power of modern physics-based animation and ease of using hooks along with nice concise syntax. It has a simple and intuitive API and a wide collection of pre-build animation components.
  3. Small package size: React Spring is a relatively small library, so it won’t add a lot of bloat to your application allowing the applications to load quickly and perform well on low-end devices.
  4. Effortlessly create interactive animation: React Spring makes it very easy to create interactive animations that respond to user input. For example, you can create animations that respond to drag and swipe gestures.
  5. Well Documentation: The documentation for React is designed to be highly visual and interactive, with plenty of examples and demos that show how to use the library in action. By showing rather than telling, the documentation helps newbies quickly understand and experiment with different animations and configurations. They also provide clear explanations of the underlying physics principles and concepts and how things work under the hood.

Getting Started

Here’s how you define and use a basic fade animation.

  1. 1nstall React Spring

yarn add @react-spring/web
OR
npm add @react-spring/web

2. Import the necessary components
{useSpring, animated } are some of the most commonly used ones.

import { useSpring, animated } from 'react-spring';

3. Define your animation using the useSpring hook.
It returns an object with animated values that you can use to update the styles of your components.

function App() {
const fade = useSpring({
from: { opacity: 0 },
to: { opacity: 1 },
delay: 100,
config: { duration: 1000 },
});
return (

Hello, World!

);
}

A Basic Spring

**useSpring** is a very basic hook in React Spring that allows you to animate the values of your components. It is an that creates an object full of animation ie.

  1. Define the animation, most common properties include:

  2. from: Starting value of the animation set to the initial state of the component.

  3. to: Ending value of the animation is usually set to its final state.
  4. config: The animation configuration object that controls the properties like duration, easing, and delay of the animation.

In the example above, we have a simplefade animation which specifies that the opacity of the div should transition from 0 to 1 over a duration of 1000 milliseconds with a delay of 100 milliseconds.

  1. Using the animation

Wrap the component you want to animate with the animated component (previously imported from React-spring). Now, We can use the animation defined previously just by passing it to the style prop of the animated.div.

In the above example, we put style = {fade} in the div, we want to apply animation to and wrapped it with animated.

animated is a Higher Order Component (HOC) provided by React-spring. It is used as a wrapper for elements, divs, or components you wanna use your animation on.

You can also use useSpring to create more complex animations, such as moving or scaling components, changing their colors, or animating them based on user interactions and the sky is the limit.
I hope this article was helpful to get you started.

If you want to explore further, here’s a link to their official docs:
https://www.react-spring.dev/docs/getting-started.