How to Loop an Animation with react-spring useSpring
Repeating, reversing and chaining animations with the loop prop.
There are times when you need an animation to keep running, like a pulsing
notification dot or a loading indicator that bounces back and forth. With
react-spring v9 you don't need an interval or a callback that restarts the
animation. useSpring has a loop prop that handles it for you.
TL;DR Pass loop: true to repeat an animation from the start, or
loop: { reverse: true } to play it back and forth.
Basic Looping with useSpring
First install react-spring:
npm install react-spring
Then pass from, to and loop to useSpring, and spread the result onto an
animated element:
import { useSpring, animated } from "react-spring"export default function Spinner() {const styles = useSpring({from: { rotate: 0 },to: { rotate: 360 },loop: true,})return <animated.div className="spinner" style={styles} />}
useSpring returns animated values, and animated.div applies them to the
element without re-rendering your component on every frame. rotate is one of
the transform shorthands v9 supports, along with x, y and scale.
With loop: true the animation jumps back to the from values and plays again
every time it finishes.
Looping Back and Forth
Jumping back to the start works for a spinner, but a pulse should grow and then
shrink. Pass an object with reverse: true and each loop swaps from and to:
const styles = useSpring({from: { scale: 1, opacity: 1 },to: { scale: 1.2, opacity: 0.6 },loop: { reverse: true },})return <animated.div className="notification-dot" style={styles} />
Stopping a Loop
loop can also be a function. It runs each time the animation finishes, and
the animation only plays again if it returns true. Here a ref keeps count so
the wiggle repeats three times and then stops:
import { useRef } from "react"import { useSpring, animated } from "react-spring"export default function Wiggle({ children }) {const count = useRef(0)const styles = useSpring({from: { x: 0 },to: { x: 10 },loop: () => count.current++ < 3,})return <animated.div style={styles}>{children}</animated.div>}
Chaining Steps
to also accepts an array. Each step plays after the one before it finishes,
and with loop: true the whole sequence repeats:
const styles = useSpring({from: { x: 0, y: 0 },to: [{ x: 40 }, { y: 40 }, { x: 0 }, { y: 0 }],loop: true,})
This moves the element around a square, one side at a time.
Changing the Feel with config and delay
config controls how the animation moves. react-spring comes with presets like
config.wobbly and config.slow, or you can pass a duration in milliseconds
when you want a fixed timing instead of a spring. delay sets how many
milliseconds to wait before the animation starts:
import { useSpring, animated, config } from "react-spring"const bounce = useSpring({from: { y: 0 },to: { y: -20 },loop: { reverse: true },config: config.wobbly,})const fade = useSpring({from: { opacity: 0.2 },to: { opacity: 1 },loop: { reverse: true },config: { duration: 800 },delay: 500,})
Respecting Reduced Motion
An animation that never stops can be uncomfortable for some people, so it's a
good idea to turn it off when someone has asked their system to reduce motion.
A small hook can read the prefers-reduced-motion media query:
import { useEffect, useState } from "react"function usePrefersReducedMotion() {const [reduced, setReduced] = useState(false)useEffect(() => {const query = window.matchMedia("(prefers-reduced-motion: reduce)")setReduced(query.matches)const onChange = (event) => setReduced(event.matches)query.addEventListener("change", onChange)return () => query.removeEventListener("change", onChange)}, [])return reduced}
Then skip the loop and set immediate so the element goes straight to its end
state without animating:
const reduced = usePrefersReducedMotion()const styles = useSpring({from: { scale: 1 },to: { scale: 1.2 },loop: reduced ? false : { reverse: true },immediate: reduced,})
That's all it takes. One prop replaces the timers and restart logic you'd otherwise write yourself.
Have some feedback on this post? .