Today I was looking to add some animations to my project, and tripped over this Spring package for React. I found that it did the job far better than trying to use the traditional CSS animations in React.
To use Spring, you first need to install it with:
npm i react-spring
Then on your component you'll need to import it
import useSpring,
animated from 'react-spring';
Then we create an object with the styles that we want. The Spring library will
then animate our elements with this object whenever the page is re-rendered.
In this case the variables topDist and leftDist are taken from a useState, and
whenever they're changed, it will animate.
const springProps =
useSpring(
top: topDist + "px",
left: leftDist + "px"
);
Finally, on the HTML (return function) we use the animated function, and set
its style to what we've declared.
<animated.div className="pawn"
style=springProps />
Here's a working example:
In the case we want to use it on multiple elements, we could use multiple useSpring's, or we can use the useSprings.
First, we'll need to import the import from useSpring to useSprings. Then we'll need an array with multiple entries, like:
Then we'll useSprings instead of useSpring. In this case, for simplicity,
we'll just make the 2nd circle circle to move twice as much as the 1st one.
And finally we update the render function: