React Native Animated.Value — Complete Guide
In this tutorial, you'll learn about React Native Animated.Value. We cover key concepts, practical examples, and best practices.
The Problem
Your Animated.Value animations jank on scroll, or interpolate produces unexpected values.
Wrong Approach ❌
const fadeAnim = new Animated.Value(0); // Created outside component — never updates
function FadeView() {
useEffect(() => {
Animated.timing(fadeAnim, {
toValue: 1,
duration: 1000,
}).start();
}, []);
return (
<Animated.View style={{ opacity: fadeAnim }}>
<Text>Hello</Text>
</Animated.View>
);
}
Output: Animation works for one instance but breaks when remounted.
Right Approach ✅
import { useRef, useEffect, useCallback } from 'react';
import { Animated, Easing, View, Text, StyleSheet, PanResponder } from 'react-native';
// useAnimatedValue hook
function useAnimatedValue(initialValue: number = 0): Animated.Value {
const animatedValue = useRef(new Animated.Value(initialValue)).current;
return animatedValue;
}
function FadeInView({ children, delay = 0 }) {
const opacity = useAnimatedValue(0);
useEffect(() => {
const animation = Animated.timing(opacity, {
toValue: 1,
duration: 500,
delay,
easing: Easing.inOut(Easing.ease),
useNativeDriver: true, // CRITICAL for performance!
});
animation.start();
return () => animation.stop();
}, [delay]);
return (
<Animated.View style={{ opacity }}>
{children}
</Animated.View>
);
}
// Interpolation example
function ProgressBar({ progress }: { progress: Animated.Value }) {
const width = progress.interpolate({
inputRange: [0, 1],
outputRange: ['0%', '100%'],
extrapolate: 'clamp', // Don't exceed bounds
});
const color = progress.interpolate({
inputRange: [0, 0.5, 1],
outputRange: ['#FF3B30', '#FF9500', '#34C759'],
});
return (
<View style={styles.progressContainer}>
<Animated.View
style={[
styles.progressBar,
{ width, backgroundColor: color },
]}
/>
</View>
);
}
// Parallel animations
function ParallelAnimations() {
const scale = useAnimatedValue(0);
const rotate = useAnimatedValue(0);
const opacity = useAnimatedValue(0);
const animate = useCallback(() => {
Animated.parallel([
Animated.spring(scale, { toValue: 1, useNativeDriver: true }),
Animated.timing(rotate, {
toValue: 1,
duration: 1000,
useNativeDriver: true,
}),
Animated.timing(opacity, {
toValue: 1,
duration: 500,
useNativeDriver: true,
}),
]).start();
}, []);
const rotation = rotate.interpolate({
inputRange: [0, 1],
outputRange: ['0deg', '360deg'],
});
return (
<Animated.View
style={[
styles.box,
{
opacity,
transform: [{ scale }, { rotate: rotation }],
},
]}
>
<Text onPress={animate}>Tap to animate</Text>
</Animated.View>
);
}
// Animation with PanResponder
function DraggableBox() {
const pan = useAnimatedValue(0);
const panY = useAnimatedValue(0);
const panResponder = PanResponder.create({
onMoveShouldSetPanResponder: () => true,
onPanResponderMove: Animated.event([
null,
{ dx: pan, dy: panY },
], { useNativeDriver: false }), // Position needs native driver false
onPanResponderRelease: () => {
Animated.spring(pan, { toValue: 0, useNativeDriver: false }).start();
Animated.spring(panY, { toValue: 0, useNativeDriver: false }).start();
},
});
return (
<Animated.View
style={{ transform: [{ translateX: pan }, { translateY: panY }] }}
{...panResponder.panHandlers}
>
<View style={styles.box} />
</Animated.View>
);
}
const styles = StyleSheet.create({
box: { width: 100, height: 100, backgroundColor: 'blue' },
progressContainer: { height: 4, backgroundColor: '#E0E0E0', borderRadius: 2 },
progressBar: { height: '100%', borderRadius: 2 },
});
Output: Smooth animations with native driver and proper interpolation.
Prevention
- Use
useNativeDriver: truefor transform/opacity animations (not position). - Use
useRefto persistAnimated.Valueacross renders. - Use
interpolatewithextrapolate: 'clamp'for bounded values. - Use
Animated.parallel,sequence,staggerfor complex animations. - Clean up animations in
useEffectreturn.
Common Mistakes with native animated value
- Misunderstanding that
Stringis[Char]with poor performance for large text operations - Using
foldlinstead offoldl'causing stack overflow on large lists - Forgetting
deriving (Show, Eq)on custom data types needed for debugging
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