tezvyn:

Animated.Value: The Driver for React Native Animations

AI-drafted, machine-checkedSource: reactnative.devbeginner

Animated.Value is a single number that drives multiple UI properties, like one dimmer switch controlling many lights. Use it to fade, move, or scale components. The main footgun: you cannot read its value synchronously and must use an asynchronous listener.

WHY IT EXISTS To create performant animations, React Native needs to offload work from the JavaScript thread. By declaring an animation with a driver like Animated.Value, the entire animation can be serialized and sent to the native UI thread to run smoothly, independent of JS workload, ensuring a fluid 60 FPS experience.

THE MENTAL MODEL Think of an Animated.Value as a single dimmer switch. This switch doesn't control a light directly; it just holds a number (e.g., from 0 to 1). You can then connect this one switch to multiple things: a light's brightness, a speaker's volume, and a fan's speed. As you turn the dimmer, all connected properties change in a synchronized way. In React Native, the "switch" is your Animated.Value, and the "connections" are UI style properties like opacity, scale, or position.

HOW IT WORKS You initialize a value, typically with useAnimatedValue(0). Then, you use an animation function like Animated.timing() to change that value over time. This value is then wired into an Animated.View's style prop. The interpolate method is the real magic: it maps the input range of your Animated.Value (e.g., 0 to 1) to a specific output range for a style property (e.g., '0deg' to '180deg' for rotation). An Animated.ValueXY is simply a convenience wrapper that bundles two Animated.Values for managing 2D properties like position (x, y).

WHEN TO USE IT Use Animated.Value for any numeric style property you want to change over time. It's the backbone for fading elements in (animating opacity), moving elements (animating transform: translateX), scaling buttons on press (transform: scale), or tracking finger movement from the PanResponder for drag-and-drop gestures.

WHEN NOT TO USE IT Avoid it for simple state changes that don't require a smooth transition. For complex, highly interactive animations, a more modern library like Reanimated might be a better choice. Also, for animations that change the layout of the screen (affecting the position or size of other elements), prefer using the dedicated LayoutAnimation API.

ONE CANONICAL EXAMPLE To fade a component in, you first create a value: const fadeAnim = useAnimatedValue(0);. Then, you define and start the animation, often in a useEffect hook or event handler: Animated.timing(fadeAnim, { toValue: 1, duration: 500, useNativeDriver: true }).start();. Finally, you apply it to an Animated.View: <Animated.View style={{ opacity: fadeAnim }}>...</Animated.View>. As fadeAnim changes from 0 to 1, the view's opacity updates accordingly.

Read the original → reactnative.dev

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.