Skip to content

React Native Animated.Value — Complete Guide

DodaTech Updated 2026-06-24 3 min read

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: true for transform/opacity animations (not position).
  • Use useRef to persist Animated.Value across renders.
  • Use interpolate with extrapolate: 'clamp' for bounded values.
  • Use Animated.parallel, sequence, stagger for complex animations.
  • Clean up animations in useEffect return.

Common Mistakes with native animated value

  1. Misunderstanding that String is [Char] with poor performance for large text operations
  2. Using foldl instead of foldl' causing stack overflow on large lists
  3. 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

### What is the native driver?

The native driver runs animations on the UI thread instead of the JS thread. Use useNativeDriver: true for opacity, transform — NOT for left, top, width, height.

### When should I use Animated.spring vs Animated.timing?

Use spring for natural, bouncy animations (like scaling a button). Use timing for linear/eased animations (like fading in). Spring uses physics, timing uses duration.

### What is the difference between Animated.Value and Animated.ValueXY?

Animated.ValueXY holds an { x, y } pair for 2D animations (position, translation). Use it with PanResponder for drag gestures.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro