Interpolation and Ranges
You'll learn how to transform spring animation values into any output range you need, enabling smooth transitions between colors, sizes, rotations, and more.
Understanding Interpolation
When you animate with React Spring, you often get numeric values that increment smoothly. But what if you need to animate a color from blue to red, or rotate an element based on scroll position? Interpolation lets you map these numeric spring values to different output ranges.
Think of interpolation like a translator: your spring produces numbers from 0 to 1, but interpolation converts those numbers into whatever your UI needs—degrees of rotation, opacity values, RGB colors, or even custom strings.
Basic Range Mapping
React Spring's animated values can be interpolated using the .to() method. You define an input range (the spring's values) and an output range (what you want those values to become):
const { scroll } = useSpring({ scroll: 0 });
const opacity = scroll.to([0, 100], [1, 0]);
// When scroll=0 → opacity=1
// When scroll=100 → opacity=0
Derived Animations
You can create derived animations—new animated values calculated from existing springs without creating additional springs. This is memory-efficient and keeps animations in sync:
const { x } = useSpring({ x: progress });
const rotate = x.to(val => `rotate(${val * 360}deg)`);
const backgroundColor = x.to([0, 1], ['#3498db', '#e74c3c']);
The function form x.to(val => ...) gives you complete control over transformation logic.
Key Takeaway: Interpolation with .to() transforms spring values into any output range, letting you create complex, coordinated animations from simple numeric springs without performance overhead.