Animated
React Native's Animated API. An animation runs in JavaScript on each frame, and a frame writes the new style to the element, with no React render. With useNativeDriver: true, a timing or a spring of transform and opacity runs as a Web Animation. See useNativeDriver.
import { useEffect, useState, type ReactNode } from 'react'
import { Animated } from 'react-native'
export function FadeIn({ children }: { children: ReactNode }) {
const [opacity] = useState(() => new Animated.Value(0))
useEffect(() => {
Animated.timing(opacity, { toValue: 1, duration: 300, useNativeDriver: true }).start()
}, [opacity])
return <Animated.View style={{ opacity }}>{children}</Animated.View>
}
What's there
| Member | On the web |
|---|---|
new Animated.Value(value) | setValue, setOffset, flattenOffset, extractOffset, stopAnimation, resetAnimation, stopTracking, interpolate, and the listeners. |
new Animated.ValueXY({ x, y }?) | Two values, x and y, for a 2D motion such as a drag. The same methods take and give { x, y }. getLayout() gives { left, top }, and getTranslateTransform() gives the transform. |
new Animated.Color(color?) | Four values, r, g, b (0 to 255), and a (0 to 1), from a color string, { r, g, b, a }, or four Animated.Values. Black by default. Put it in a color style, such as backgroundColor. |
useAnimatedValue, useAnimatedValueXY, useAnimatedColor | Hooks, imported from react-native. Each makes its value on the first render and returns the same value after that. |
value.interpolate(config) | Numbers, strings with numbers such as '90deg', and colors, which come out as rgba(). extrapolate, extrapolateLeft, extrapolateRight, and easing. |
Animated.add, subtract, multiply, divide | Take two values, or a value and a number. A division by zero gives 0, as on native. |
Animated.modulo(value, modulus) | The rest from 0 up to modulus, also for a negative value. |
Animated.diffClamp(value, min, max) | Adds each change of value and keeps the sum from min to max, so the output moves as soon as value turns back. For a header that hides when you scroll down and comes back when you scroll up. |
Animated.timing(value, config) | toValue, duration (500 ms by default), easing (Easing.inOut(Easing.ease) by default), and delay. See Easing. |
Animated.spring(value, config) | stiffness, damping, and mass, or tension and friction, or bounciness and speed. Also velocity, overshootClamping, and the rest thresholds. |
Animated.decay(value, config) | Starts at velocity (units per millisecond) and slows by deceleration (0.998 by default) each millisecond. |
Animated.sequence(animations) | Starts each animation when the one before it ends. One that stops early ends the sequence. |
Animated.parallel(animations, config?) | Starts each animation at once. With stopTogether (the default), one that stops early stops the rest. |
Animated.delay(time), Animated.stagger(time, animations) | delay waits time ms, for a pause in a sequence. stagger starts animation i after time * i ms. |
Animated.loop(animation, config?) | Runs animation iterations times, or until stop() with -1 (the default). With resetBeforeIteration (the default), each iteration starts from the first value. |
Animated.event(mapping, config) | A handler that sets each mapped value from the event, then calls listener. A ValueXY in the mapping takes an { x, y } of the event. |
Animated.forkEvent(handler, listener), Animated.unforkEvent(handler, listener) | forkEvent gives a handler that calls handler and then listener. On an Animated.Event, it adds listener, and unforkEvent removes it. |
Animated.attachNativeEvent(ref, 'onScroll', mapping) | Sets the values in mapping[0].nativeEvent from each scroll event of the element of ref, with no event prop. Returns { detach }. |
Animated.View, Animated.Text, Animated.Image, Animated.ScrollView | A value in style or in transform animates. |
Animated.FlatList, Animated.SectionList | The lists, with animated props. For onScroll={Animated.event(...)}, as on native. |
Animated.createAnimatedComponent(Component) | Animates the props of any component. |
Animated.Node, Animated.Interpolation | The classes, for types and instanceof. |
Each animation has start(callback?), stop(), and reset().
The toValue of timing and spring can be another Animated value or an interpolation. The animated value then follows it: each change starts a new animation to the new target. stopAnimation() or a new animation of the value ends this. setValue() doesn't.
timing, spring, and decay also animate a ValueXY or a Color: they run one animation for each channel. A toValue or a velocity with a part for each channel, such as { x: 100, y: 0 } or { r: 255, g: 0, b: 0, a: 1 }, gives each channel its part. Another ValueXY or Color as toValue makes each channel follow its own channel.
useNativeDriver
With useNativeDriver: true, a timing or a spring runs as a Web Animation when each style that its value moves is transform or opacity. The browser plays the animation, and it can go on while the main thread is busy, for example while a long list renders. No JavaScript writes the style on each frame.
- During the animation, the value gives the value of the current frame, and its listeners get each frame.
- Two values on one element share one Web Animation, such as the
xandyof aValueXY, or a fade and a move in aparallel. sequence,stagger, andlooprun eachtimingandspringin them in the same way.- At the end, and at a stop, the value goes into the inline style of the element.
In these cases, the animation runs on the JavaScript driver, with the same result:
- The value moves a style other than
transformandopacity, such asbackgroundColor, or a prop that isn'tstyle. - The value goes through
diffClamp, or another value follows it as itstoValue. - The component doesn't give its
refto an element. - During the animation, a value on the JavaScript driver changes the same element, for example with
setValue. The animation then goes on with the JavaScript driver to its end. - The animation moves many elements for a long time: more than 3,000 frames at 60 Hz for all its elements together, such as 1 s on 50 elements.
- The animation is a
decay, or the value comes fromAnimated.event. - The browser has no Web Animations API, as in jsdom.
React Native's rules for the native driver apply:
- In development, a style key, a transform, or an
interpolateoption that React Native's native driver can't move logs React Native's error, such asStyle property 'width' is not supported by native animated module. The animation still runs on the web. - A value moves to the native side with one of these. After that, an animation of the value with
useNativeDriver: falsethrows React Native's error.- An animation of the value with
useNativeDriver: true. useNativeDriver: truein the config ofnew Animated.Value,new Animated.ValueXY, ornew Animated.Color, or of their hooks.- The value is under
nativeEventin the mapping ofAnimated.eventwithuseNativeDriver: true, or ofAnimated.attachNativeEvent. React Native moves it when the event attaches to a view. Here it moves at once, becauseAnimated.eventreturns a function.
- An animation of the value with
Differences from React Native
useNativeDriver: truemoves onlytransformandopacityon a Web Animation. Any other style runs on the JavaScript driver. SeeuseNativeDriver.- During a
useNativeDriver: trueanimation, the value gives the value of the current frame, andstopAnimation(callback)calls back at once. On native, the JavaScript value stays at the start until the end, and the callback comes later. - Reduced motion. When the user asks for less motion (
prefers-reduced-motion),timingandspringgo totoValueat once, anddecaygoes at once to the value where it would stop. Adelaystill waits, so a sequence keeps its pauses. Aloopends after one iteration. - An
{ x, y }with a singleAnimated.Valueas thetoValueor thevelocitythrows an error. React Native givesNaNframes. - A color that
Animated.Colorcan't read gives black, as on native. This includes CSS values such asvar(--accent). - A division by zero logs one warning in development. React Native logs an error each time.
- A value in a prop that isn't
stylerenders the component again on each frame. A value instyledoesn't. - A value in the
styleofAnimated.FlatListorAnimated.SectionListrenders the list again on each frame, as React Native does on the New Architecture. To animate a list cheaply, put the value on anAnimated.Viewaround the list. Animated.eventalways returns a function, also withuseNativeDriver: true. SoforkEventof it gives a new function.attachNativeEventtakes onlyonScroll. React Native attaches the values to a native event, with no JavaScript. The web listens to thescrollevent of the element in JavaScript. Any other event name has no effect and logs a warning in development.