useTrail for Staggered Animations
What you'll learn: How to use React Spring's useTrail hook to create animations where multiple elements follow each other in sequence with a smooth stagger effect.
What Is useTrail?
useTrail is React Spring's solution for animating a collection of elements where each one follows the previous, like dominoes falling or a line of dancers performing the same move with a slight delay. Unlike useSprings where you might manually configure each spring's delay, useTrail automatically handles the staggering for you—each element inherits the animation values from the one before it.
Think of it like a group of people doing "the wave" at a stadium: each person performs the same motion, but starts right after their neighbor.
How It Works
useTrail takes two key arguments:
- The count of items you want to animate
- The animation configuration (just like
useSpring)
It returns an array of animated values, one for each item, already staggered automatically.
import { useTrail, animated } from '@react-spring/web';
function StaggeredList({ items }) {
const trail = useTrail(items.length, {
from: { opacity: 0, transform: 'translateY(20px)' },
to: { opacity: 1, transform: 'translateY(0px)' },
});
return (
<div>
{trail.map((style, index) => (
<animated.div key={index} style={style}>
{items[index]}
</animated.div>
))}
</div>
);
}
Each element fades in and slides up, but each starts slightly after the previous one—creating that satisfying cascade effect.
Key Takeaway: useTrail simplifies staggered animations by automatically creating a sequence of springs where each element follows its predecessor, perfect for lists, menus, and sequential reveals.