Skip to content
Tutorials Center
My learning

Topics

Lesson 47 of 71 ยท Animation and Gestures

Animations with the Animated API

Animate React Native views with the built-in Animated API: Animated.Value, timing and spring, interpolate, the native driver and LayoutAnimation, with runnable math demos.

Motion is how an app explains itself. A panel that slides in tells you where it came from, a button that dips when pressed confirms the tap, and a list item that fades out tells you it is gone. Good animation is not decoration; it is feedback. React Native has a built-in library for it called Animated, and it is the first tool to learn before you reach for anything heavier.

Animated is declarative: you describe the start, the end and how to travel between them, and the library runs the frames. In this lesson you will meet Animated.Value, the timing and spring animations, interpolate, the useNativeDriver flag that keeps motion smooth, and LayoutAnimation for layout changes.

Read-only and stand-in. Real animations drive native views at 60 frames per second, which a browser tab cannot reproduce. The React Native code is shown in plain blocks. The TryIts run the math behind it: interpolation, easing curves and a spring simulation, rendered as tables and charts.

The core idea: a value that drives a style

You do not change a style property on every frame yourself. Instead you create an Animated.Value, attach it to a style on an animated component, and start an animation that changes the value. React Native updates the view without re-rendering your component.

TSX
import { useRef } from 'react';
import { Animated, Button, View } from 'react-native';

export default function FadeBox() {
  const opacity = useRef(new Animated.Value(0)).current;

  function fadeIn() {
    Animated.timing(opacity, {
      toValue: 1,
      duration: 400,
      useNativeDriver: true,
    }).start();
  }

  return (
    <View>
      <Animated.View
        style={{ width: 120, height: 120, backgroundColor: 'tomato', opacity }}
      />
      <Button title="Fade in" onPress={fadeIn} />
    </View>
  );
}

Four details deserve attention.

  • The value lives in a ref, so it survives re-renders and is created once. Putting new Animated.Value(0) straight into the component body would reset it on every render.
  • Only animated components accept animated values: Animated.View, Animated.Text, Animated.Image, Animated.ScrollView, and Animated.FlatList. Wrap other components with Animated.createAnimatedComponent.
  • Changing the value does not re-render your component. That is the point: the animation runs outside the React render cycle.
  • start() accepts a callback that receives { finished }, which is false if something stopped the animation early.

timing and spring

There are two workhorse animations.

Animated.timing moves a value to a target over a fixed duration, shaped by an easing function.

TSX
import { Easing } from 'react-native';

Animated.timing(position, {
  toValue: 200,
  duration: 300,
  easing: Easing.out(Easing.cubic), // fast start, gentle stop
  useNativeDriver: true,
}).start();

Easing.out slows the end (good for things arriving on screen), Easing.in slows the start (good for things leaving), and Easing.inOut does both. A linear animation, with no easing, looks mechanical.

Animated.spring simulates a physical spring and has no fixed duration: it runs until it settles.

TSX
Animated.spring(scale, {
  toValue: 1,
  stiffness: 180,
  damping: 12,
  mass: 1,
  useNativeDriver: true,
}).start();

A stiffer spring moves faster, more damping removes the bounce, and more mass makes it feel heavier. Springs shine for interactions that respond to a finger, because they can start from whatever velocity the value already has, so interrupting a spring and reversing it still looks natural. A timing animation that restarts mid-flight jumps to a fresh curve.

You can also combine animations: Animated.sequence([...]) runs them one after another, Animated.parallel([...]) runs them together, Animated.stagger(ms, [...]) starts each a little later than the previous one, Animated.delay(ms) waits, and Animated.loop(animation) repeats forever.

interpolate: one value, many styles

One animated number can drive several properties through interpolate. You provide an input range and an output range, and in-between values are computed linearly.

TSX
const progress = useRef(new Animated.Value(0)).current; // runs from 0 to 1

const translateY = progress.interpolate({
  inputRange: [0, 1],
  outputRange: [40, 0], // slide up by 40
});
const rotate = progress.interpolate({
  inputRange: [0, 1],
  outputRange: ['0deg', '180deg'], // strings with units work too
});

<Animated.View
  style={{
    opacity: progress,
    transform: [{ translateY }, { rotate }],
  }}
/>;

Outputs may be numbers, angles like '45deg', or colors. Input ranges can have more than two points, such as [0, 0.5, 1] mapped to [0, 1, 0] for a pulse. By default values extrapolate, meaning an input past the range continues the line. Pass extrapolate: 'clamp' to stop at the ends, which is what you want for scroll-linked effects such as a header that shrinks and then stays small.

Scroll position is the classic input:

TSX
const scrollY = useRef(new Animated.Value(0)).current;

<Animated.ScrollView
  onScroll={Animated.event([{ nativeEvent: { contentOffset: { y: scrollY } } }], {
    useNativeDriver: true,
  })}
  scrollEventThrottle={16}
/>;

One value, three jobs

Animated.Value A number you animate over time.
timing or spring How the number travels.
interpolate Maps the number to styles.

Attach the value to an Animated component and call start().

1 / 2

The native driver

By default the JavaScript thread computes every frame and sends the result to the UI thread. If your JavaScript is busy (parsing a response, rendering a long list), frames are late and the animation stutters.

Setting useNativeDriver: true sends the whole animation description to the native side once. The UI thread then runs it alone, so a blocked JavaScript thread cannot make it drop frames. The trade-off is a restricted set of properties. Safe choices are transform (translate, scale, rotate) and opacity. Layout properties such as width, height, margin and top cannot use it, because changing them needs a layout pass, which belongs to a different system.

Two practical rules follow from this:

  1. Animate with transform and opacity whenever you can. Slide with translateX instead of changing left; grow with scale instead of changing width.
  2. A given value is either native-driven or JavaScript-driven; do not mix both drivers on the same value. Pick one when you first attach it.

On the web build of Expo, the native driver does not exist and the flag falls back to JavaScript with a warning, so you may see a message there that you can safely ignore.

LayoutAnimation

Sometimes the thing that changes is not a value but the layout: an item is added to a list, a section expands, a row is deleted. LayoutAnimation animates those changes automatically. You ask for an animation before the state change, and the next layout is animated.

TSX
import { LayoutAnimation } from 'react-native';

function toggleDetails() {
  LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut);
  setExpanded((value) => !value);
}

It is the shortest path to a smooth expand and collapse, and it needs no Animated.Value. Its limit is control: you pick a preset or a simple configuration, and everything that changes in the next layout animates. On Android it historically required an opt-in call (UIManager.setLayoutAnimationEnabledExperimental(true)), so check the notes for your React Native version if nothing animates there. When you need precise control over individual properties, use Animated values, or the Reanimated library covered in Reanimated and Gesture Handler.

Stand-in: interpolation

The first TryIt implements interpolate for numbers and angle strings, with and without clamping, and prints a table so you can see how one input value becomes several style values. It is a re-creation of the math, not the real library.

How interpolate maps one value to styles

Look at the first and last rows. Without clamping, a progress of -0.5 or 1.5 pushes translateY past its range, to 60 and -20. With 'clamp', the value stays at 40 or 0. The pulse column shows a three-point range: it rises to 1 at the midpoint and falls back to 0.

Stand-in: timing versus spring

The second TryIt computes the two animation families from scratch and draws them. The timing curve uses a fixed duration and an ease-out cubic. The spring uses a damped oscillator that you step forward in small time slices, which is the physical idea behind Animated.spring (the real library's solver is more careful, so treat the numbers as an illustration).

Timing and spring curves, computed and drawn

The timing curve never goes beyond the target; it just eases in. The bouncy spring overshoots the target before settling, and the firmer spring with double the damping barely does. Springs have no duration of their own: the "frames to settle" column is a result of the physics, not a setting.

Choosing the right tool

  • A simple fade, slide or scale triggered by a tap: Animated.timing or spring with the native driver.
  • An interaction that follows a finger and can be interrupted: spring, or a gesture library (see Reanimated and Gesture Handler).
  • Expanding sections and list changes: LayoutAnimation.
  • Heavy, gesture-driven or layout-heavy motion on the UI thread: Reanimated.

Keep animations short (150 to 400 milliseconds for most UI), animate transform and opacity, and respect users who ask for reduced motion: AccessibilityInfo.isReduceMotionEnabled() tells you when to shorten or skip movement (see Accessibility in React Native).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is an Animated.Value usually created with useRef?

    A ref keeps the same Animated.Value across renders. Creating it directly in the body would reset the animation state on every render.
  2. 2. Which pair of style properties is safest to animate with useNativeDriver: true?

    Transform and opacity can be applied without a layout pass, so the UI thread can run them alone. Layout properties cannot use the native driver.
  3. 3. What is the main difference between timing and spring?

    Timing travels to the target over a set duration with an easing curve. A spring is simulated physics with no preset duration and handles interruption gracefully.
  4. 4. What does extrapolate: 'clamp' do in interpolate?

    Without clamping, the line keeps going beyond the range. Clamp pins the result to the first or last output value.

Summary

  • Animated.Value holds a number; animated components (Animated.View, Animated.Text, and so on) read it without re-rendering your component.
  • Animated.timing uses a duration and easing; Animated.spring uses physics. Combine them with sequence, parallel, stagger and loop.
  • interpolate maps one value to many styles, including angles and colors; use extrapolate: 'clamp' to stop at the ends.
  • useNativeDriver: true runs the animation on the UI thread. Prefer transform and opacity, and never mix drivers on one value.
  • LayoutAnimation animates layout changes such as expanding sections and list edits.
  • Keep motion short and honor the reduced-motion setting.

Next, take animation and touch to the UI thread in Reanimated and Gesture Handler.