A fade or a slide changes a style on every frame, and sending each of those frames through setState would put your component through a React render and reconciliation every time. The Animated API keeps the moving number outside React state and updates the bound properties in a way that is faster than calling setState and re-rendering. Every Animated animation is built from a value, a component that can receive it, and an animation that drives it.
Key terms
- Animated.Value: the animated type that holds a single number, which you bind to style properties or other props.
- useAnimatedValue: a Hook exported by
react-nativethat creates anAnimated.Valueon the first render and returns that same instance on later renders. - Animatable component: a component such as
Animated.Viewthat binds animated values to its properties and updates the native view without a React render on each frame. - CompositeAnimation: the object that
Animated.timing()and the other animation functions return, withstart(),stop()andreset()methods.
One value, bound once, driven many times
You create an Animated.Value, hook it to one or more style attributes of an animatable component, then drive it with an animation such as Animated.timing(). The binding happens once, in your JSX. After that you work with the value and the animation, and the component picks up each new number.
A single value can feed any number of properties. One value going from 0 to 1 can drive a card's opacity and a scale transform together, and both stay in sync because they read the same number. Only one animation runs on a value at a time, though. Starting a new animation on it, or calling setValue(), stops whatever was already running on it.
While a value animates, it may only be known on the native side, so there is no clear way to read it synchronously. To observe it, addListener() registers an asynchronous listener that receives the updates.
Creating the value with useAnimatedValue
In a function component, create the value with useAnimatedValue(0), imported from react-native next to Animated. It takes the initial number and an optional config, and returns an Animated.Value. It keeps the value in a ref that starts as null and only builds a new Animated.Value while that ref is empty, so every later render gets the same instance back.
Class components use new Animated.Value(0). You will also meet useRef(new Animated.Value(0)).current in function components, the pattern the Animations guide uses, which stores the value in a ref object that persists through the component lifecycle. Whichever you use, don't modify the animated value directly. The constructor also throws when the initial value is not a number.
Only animatable components move
A value changes nothing on screen until an animatable component binds it. Only animatable components can be animated: they connect animated values to properties, make targeted native updates instead of running a React render and reconciliation on each frame, and clean up on unmount.
Animated exports six of them: Animated.View, Animated.Text, Animated.Image, Animated.ScrollView, Animated.FlatList and Animated.SectionList. The View, Text, Image and ScrollView variants accept an Animated.Value for props and style. Animated.View is the plain View wrapped with Animated.createAnimatedComponent(), and that same function makes any other React component animatable, as in Animated.createAnimatedComponent(Pressable).
So when an animation seems to have no effect, check the element first. It has to be the Animated. version, with the value passed straight into its style, as in style={{opacity}}.
Starting, stopping and resetting
Building an animation doesn't run it. Animated.timing(value, config) returns a CompositeAnimation, and nothing moves until you call start() on it. start() takes an optional completion callback, called at most once: with {finished: true} when the animation ran to the end, and with {finished: false} when stop() ended it early, for example because a gesture or another animation interrupted it.
Every animation config also needs useNativeDriver set to true or false. Leave it out and the animation falls back to the JS driver, with a warning and a TypeScript type error. Opacity and transforms can run on the native driver, and a later lesson covers what that changes.
The ToastMessage component below fades a toast in when it mounts and lets the user tap to skip the fade. Look at where start(), stop() and setValue() are called.
The binding is style={{opacity}} on Animated.View. The effect cleanup, return () => fadeIn.stop();, stops the fade when the toast unmounts halfway through, and the callback then takes the finished: false branch. The tap handler calls opacity.setValue(1), which stops the running fade, sets the value directly and updates every bound property at once.
reset() on the animation stops it and puts the value back to its original. On the value, stopAnimation(callback) stops any running animation and passes the final value to the callback, which helps when you need to know where the motion stopped. resetAnimation() stops it and returns the value to the number it was created with.
Code Examples
import {useEffect} from 'react';
import {Animated, Pressable, Text, useAnimatedValue} from 'react-native';
export function ToastMessage({message}: {message: string}) {
const opacity = useAnimatedValue(0);
useEffect(() => {
const fadeIn = Animated.timing(opacity, {
toValue: 1,
duration: 600,
useNativeDriver: true,
});
fadeIn.start(({finished}) => {
console.log(finished ? 'toast fully shown' : 'fade-in stopped early');
});
return () => fadeIn.stop();
}, [opacity]);
return (
<Pressable onPress={() => opacity.setValue(1)}>
<Animated.View style={{opacity}}>
<Text>{message}</Text>
</Animated.View>
</Pressable>
);
}