useChain: Sequencing Multiple Animations
What you'll learn: How to orchestrate multiple React Spring animations in a precise sequence using useChain to control their timing relationships.
Why useChain Exists
You've learned useSpring, useSprings, and useTrail — but what if you need multiple separate animation groups to fire in a specific order? Imagine a modal that fades in, then its items stagger in, then a button appears. That's where useChain shines.
How useChain Works
Think of useChain as a conductor for your animation orchestra. Each animation hook returns a reference (via useSpringRef), and useChain triggers them in the order you specify.
The pattern:
- Create refs with
useSpringRef()for each animation - Pass each ref to its corresponding hook via the
refoption - Call
useChain([ref1, ref2, ref3])to sequence them - Optionally add time offsets for precise control
import { useSpring, useTrail, useChain, useSpringRef, animated } from '@react-spring/web';
function SequencedAnimation() {
// Step 1: Create refs
const fadeRef = useSpringRef();
const trailRef = useSpringRef();
// Step 2: Hook refs into animations
const fade = useSpring({
ref: fadeRef,
from: { opacity: 0 },
to: { opacity: 1 }
});
const trail = useTrail(3, {
ref: trailRef,
from: { x: -20 },
to: { x: 0 }
});
// Step 3: Sequence them — fade first, trail second
useChain([fadeRef, trailRef], [0, 0.5]); // Trail starts 0.5s after fade
return (
<animated.div style={fade}>
{trail.map((style, i) => (
<animated.div key={i} style={style}>Item {i}</animated.div>
))}
</animated.div>
);
}
The second argument [0, 0.5] controls timing: fade starts immediately (0), trail at 50% through the total duration (0.5).
Key Takeaway: useChain lets you choreograph multiple independent React Spring animations into a coordinated sequence by referencing each animation and defining their start times.