React has been one of our favorite tools for creating interfaces for a while now. In order to make apps more playful and engaging, we’ve tested several animation libraries, including Anime.js, Popmotion, and the focal point of this blog post: react-spring. The latter turned out to be a fantastic tool for building lightweight animations that feel natural.
I’m Martin, a lead frontend developer at Apptension, and in this blog post, I’ll guide you through the basics of react-spring. Let’s start with a short overview of this library.
React-spring is a spring-physics based animation library. Its distinguishing features are:
It’s also multiplatform, which means it can be used for web as well as mobile React Native apps or to animate any values with universal export(lack of native elements and color interpolation).
Essentially, it’s a library that can fundamentally change your approach to animating elements. It will also allow you to achieve smooth, natural-looking animations. It’s possible thanks to spring-physics, the basis of this library.
Instead of relying on time-based animations, react-spring uses physical properties:
In my experience, it’s easier to define motion precisely using physical attributes like these. On top of that, react-spring animations are highly responsive—when a value changes, the animation follows to the new point naturally, without any cuts.
Let me reiterate: spring-physics-based animations look really natural. For instance, when your animation stops before completion and changes direction, the movement will be caused by the opposite force and will continue naturally till it achieves the next value. With time-based animations, the movement would be stopped and restarted with new values.
Still, react-spring allows you to create time-based animations when you feel like it.
The list of elements that you can animate using react-springs includes:
The Common API is a config that is the same between all the react-spring hooks. It allows us to define proper animation behavior and access the basic properties, e.g., from/to/reset/onRest.
In order to animate, you have to import a special factory—animated. It extends native elements to handle animated values. You can wrap an existing component or a DOM element. Animated can also be used with styled components or elements directly exported from animated, e.g., “animated.div”.
To create a config, you can also use one of the generic presets imported from react-spring.
Interpolation is supported out-of-the-box in react-spring. You have a couple of options for interpolating single values:
You can also combine values from different spring definitions into one value using an “interpolate” helper available in the react-spring package.
As of now, there are five hooks in react-spring. Let’s go through them one by one.
useSpring is a basic hook that allows you to animate values with a single spring definition passing properties and the config. You can also use it to define async/await scripts and chains when you need a couple of steps in the movement.
useSprings is a hook for creating multiple springs. For each of them, you can define a different config.
useTrail is another hook for creating multiple springs. This one is used when every element should follow the previous one, e.g., for stagger animations. Here, you can only define one spring config.
useTransition is a hook for creating a spring that defines the element transition. It supports trails with a strictly defined delay between elements. Here, you have an extra config allowing you to determine the animation in the from/enter/update and leave states. With this hook, you can create atomic transitions as well as transitions on multiple elements. Similarly to useSpring, useTransitions enables you to define async/await scripts and chains.
useChain is a hook you can use for combining multiple react-spring hooks animations. For instance, you can combine a spring animation with a transition. It allows you to define a moment in time when the next chain element animation should be fired.
It’s important to remember that react-spring doesn’t rely on React to render updates frame-by-frame. As a result, animations are more performant.
Still, when you render a large number of elements, make sure to use the useMemo React hook. You might also want to use a “native” flag for render-props API to achieve better performance.
I hope that my blog post and the examples I’ve put here help you make sense of react-spring. If you’re anything like me, you’ll definitely appreciate the precision and easiness with which you can animate elements using this library.