React Native Animated Spring — Complete Guide
In this tutorial, you'll learn about React Native Animated Spring. We cover key concepts, practical examples, and best practices.
The Problem
Your spring animation overshoots too much, or it doesn't spring at all (moves linearly).
Wrong Approach ❌
Animated.spring(value, {
toValue: 1,
useNativeDriver: true,
}).start();
Output: Default spring is very bouncy. May overshoot noticeably.
Right Approach ✅
import { Animated, Easing, useRef, useEffect } from 'react';
import { View, Text, TouchableOpacity, StyleSheet } from 'react-native';
function SpringAnimations() {
const scale = useRef(new Animated.Value(1)).current;
const position = useRef(new Animated.Value(0)).current;
// Button press animation
const pressIn = () => {
Animated.spring(scale, {
toValue: 0.95,
useNativeDriver: true,
// Tension controls stiffness (default 40)
tension: 100, // Higher = stiffer, less bouncy
// Friction controls damping (default 7)
friction: 3, // Lower = more bouncy
// Velocity initial velocity (default 0)
velocity: 0,
// Speed and bounciness (alternative API)
speed: 8, // Default 12
bounciness: 4, // Default 8
}).start();
};
const pressOut = () => {
Animated.spring(scale, {
toValue: 1,
useNativeDriver: true,
friction: 5,
}).start();
};
// Drag release animation (fling)
const simulateFling = () => {
// Reset position
position.setValue(0);
Animated.spring(position, {
toValue: 200,
useNativeDriver: true,
velocity: 1.5, // Initial velocity (simulating fling)
tension: 20,
friction: 8,
// Rest displacement threshold
restDisplacementThreshold: 0.1,
// Rest speed threshold
restSpeedThreshold: 0.1,
}).start();
};
// Comparison of spring configurations
function SpringDemo() {
const anim1 = useRef(new Animated.Value(0)).current; // Default spring
const anim2 = useRef(new Animated.Value(0)).current; // Gentle spring
const anim3 = useRef(new Animated.Value(0)).current; // Stiff spring
const startAll = () => {
Animated.parallel([
Animated.spring(anim1, { toValue: 1, useNativeDriver: true }),
Animated.spring(anim2, {
toValue: 1, useNativeDriver: true,
friction: 10, tension: 20, // Gentle
}),
Animated.spring(anim3, {
toValue: 1, useNativeDriver: true,
friction: 2, tension: 80, // Bouncy
}),
]).start();
};
return (
<View>
<TouchableOpacity onPress={startAll}>
<Text>Start Springs</Text>
</TouchableOpacity>
<Animated.View style={{ transform: [{ translateX: anim1.interpolate({ inputRange: [0,1], outputRange: [0, 100] }) }] }} />
<Animated.View style={{ transform: [{ translateX: anim2.interpolate({ inputRange: [0,1], outputRange: [0, 100] }) }] }} />
<Animated.View style={{ transform: [{ translateX: anim3.interpolate({ inputRange: [0,1], outputRange: [0, 100] }) }] }} />
</View>
);
}
return (
<View style={styles.container}>
<TouchableOpacity
onPressIn={pressIn}
onPressOut={pressOut}
activeOpacity={1}
>
<Animated.View style={[styles.box, { transform: [{ scale }] }]}>
<Text>Press me</Text>
</Animated.View>
</TouchableOpacity>
<TouchableOpacity onPress={simulateFling}>
<Animated.View style={[styles.box, { transform: [{ translateX: position }] }]}>
<Text>Fling me</Text>
</Animated.View>
</TouchableOpacity>
</View>
);
}
// Use with gesture handler
function GestureSpring() {
const translateY = useRef(new Animated.Value(0)).current;
const panResponder = PanResponder.create({
onMoveShouldSetPanResponder: () => true,
onPanResponderMove: Animated.event([null, { dy: translateY }], { useNativeDriver: false }),
onPanResponderRelease: (_, gesture) => {
// If flung past threshold, animate out
if (gesture.vy < -0.5) {
Animated.spring(translateY, {
toValue: -500,
velocity: gesture.vy,
useNativeDriver: false,
}).start();
} else {
// Spring back
Animated.spring(translateY, { toValue: 0, useNativeDriver: false }).start();
}
},
});
return (
<Animated.View
style={{ transform: [{ translateY }] }}
{...panResponder.panHandlers}
/>
);
}
const styles = StyleSheet.create({
container: { flex: 1, justifyContent: 'center', alignItems: 'center' },
box: { width: 120, height: 60, backgroundColor: '#007AFF', justifyContent: 'center', alignItems: 'center', borderRadius: 8 },
});
Output: Spring animations with controlled bounciness and stiffness.
Prevention
- Use
friction(higher = less bouncy) andtension(higher = faster/stiffer). - Alternative: Use
speedandbouncinessfor a more intuitive API. - Set
restDisplacementThresholdandrestSpeedThresholdto stop animation when settled. - Match spring config to the physical behavior you want (buttons vs. fling).
Common Mistakes with native animated spring
- Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
- Non-exhaustive pattern matches that compile with warnings then crash at runtime
- Misunderstanding that
Stringis[Char]with poor performance for large text operations
These mistakes appear frequently in real-world REACT code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro