By default, Animated computes every frame of an animation on the JavaScript thread, so whatever keeps that thread busy also competes with your animation. Setting useNativeDriver: true sends everything about the animation to native code before it starts, and the UI thread runs it from there. Once it has started, the JS thread can be blocked without affecting it, which matters most for animations that follow a scroll position.

Key terms

  • useNativeDriver: the required animation config option that runs the animation through the native driver when it is exactly true.
  • Native driver: the native side of Animated, which receives the full animation before it starts and runs it on the UI thread.
  • Animated.event: a helper that maps fields of an event, such as a scroll offset, onto animated values.
  • Direct event: an event that does not bubble, such as ScrollView's onScroll, the kind Animated.event can handle on the native driver.

true, false, or a warning

useNativeDriver has no silent default. The TypeScript type of an animation config declares it as a required boolean, so leaving it out is a type error. At runtime, a config without it falls back to the JS driver and logs this warning:

text
Animated: `useNativeDriver` was not specified. This is a required option and must be explicitly set to `true` or `false`

Only the exact value true selects the native driver. Anything else, including a truthy value such as 1, runs on the JS driver.

Styles the UI thread can animate

The native driver only animates non-layout properties. transform and opacity work, while Flexbox and position properties do not. The allowlist it checks against includes:

  • the transforms translateX, translateY, scale, scaleX, scaleY, rotate, rotateX, rotateY, rotateZ, perspective, skewX and skewY
  • opacity, elevation, zIndex, filter, the border radii and, on iOS, shadowOpacity and shadowRadius
  • the colors backgroundColor, color, tintColor and the border colors

width, height, top, left, flex, margin and padding are not on it by default. Bind one to a native-driven value and, in development, Animated logs Style property 'width' is not supported by native animated module with console.error. It does not throw, so watch the logs in development.

Interpolations run natively too, with inputRange, outputRange, easing and the three extrapolation options. Degree strings are converted to radians before they reach the native side, and color outputs are converted to native color values, so a '0deg' to '360deg' rotation or a backgroundColor fade both work with useNativeDriver: true.

One value, one driver

An animated value is compatible with one driver. Once an animation with useNativeDriver: true starts on a value, the value moves to the native side, and so does every node built from it: its interpolations and the styles that use them. Starting a JS-driven animation on that value afterwards throws:

tsx
Animated.timing(badgeScale, {toValue: 1.2, duration: 150, useNativeDriver: true}).start();

// Later, on the same value: this start() throws an Error.
Animated.timing(badgeScale, {toValue: 1, duration: 150, useNativeDriver: false}).start();

The error message is:

text
Attempting to run JS driven animation on animated node that has been moved to "native" earlier by starting an animation with `useNativeDriver: true`

Calling setValue on a native value is still allowed: the new value is forwarded to the native side. Pick the driver once per value and use it for every animation on that value.

Linking a header to scroll

Without the native driver, an animation that follows the scroll position always runs a frame behind the gesture, because events reach JavaScript asynchronously. Animated.event with useNativeDriver: true lets the native side apply the scroll offset to the value directly. The mapping must put every animated value inside nativeEvent of the first argument, as in [{nativeEvent: {contentOffset: {y: scrollY}}}], and the handler goes on an animatable component such as Animated.ScrollView, which attaches the native event to its underlying native view.

On the native driver, Animated.event only works with direct events. It works with ScrollView's onScroll and does not work with PanResponder, the gesture API that a later section covers. The JavaScript handler never calls setValue in this mode. If you pass a listener, it still runs in JS with the event.

The second argument is required as well: calling Animated.event without it warns and runs on the JS driver. The scroll example in the Animated reference passes {useNativeEvent: true}, an option that doesn't exist, so copying it gives you a JS-driven event. Write useNativeDriver.

The ArticleScreen example below the lesson collapses its header, HEADER_HEIGHT tall, as the article scrolls. headerY and titleOpacity are both interpolations of scrollY with extrapolate: 'clamp', so the header stops once it is fully hidden instead of sliding on as the offset grows. Both drive translateY and opacity, which keeps the whole effect on the UI thread.

Code Examples

tsx
import {Animated, StyleSheet, Text, View, useAnimatedValue} from 'react-native';

const HEADER_HEIGHT = 120;

export function ArticleScreen({paragraphs}: {paragraphs: string[]}) {
  const scrollY = useAnimatedValue(0);

  const headerY = scrollY.interpolate({
    inputRange: [0, HEADER_HEIGHT],
    outputRange: [0, -HEADER_HEIGHT],
    extrapolate: 'clamp',
  });
  const titleOpacity = scrollY.interpolate({
    inputRange: [0, HEADER_HEIGHT / 2],
    outputRange: [1, 0],
    extrapolate: 'clamp',
  });

  return (
    <View style={styles.screen}>
      <Animated.ScrollView
        contentContainerStyle={{paddingTop: HEADER_HEIGHT}}
        onScroll={Animated.event(
          [{nativeEvent: {contentOffset: {y: scrollY}}}],
          {useNativeDriver: true},
        )}>
        {paragraphs.map((text, index) => (
          <Text key={index} style={styles.paragraph}>
            {text}
          </Text>
        ))}
      </Animated.ScrollView>
      <Animated.View style={[styles.header, {transform: [{translateY: headerY}]}]}>
        <Animated.Text style={[styles.title, {opacity: titleOpacity}]}>
          Release notes
        </Animated.Text>
      </Animated.View>
    </View>
  );
}

const styles = StyleSheet.create({
  screen: {flex: 1},
  header: {
    position: 'absolute',
    top: 0,
    left: 0,
    right: 0,
    height: HEADER_HEIGHT,
    justifyContent: 'flex-end',
    padding: 16,
    backgroundColor: 'white',
  },
  title: {fontSize: 24, fontWeight: '700'},
  paragraph: {padding: 16, fontSize: 16},
});
✓ Completed