React Native course · Module 10: Animations
Animated API - Animation basics
In this lesson3
Welcome to the Gallery of Currents! Learn to animate elements like ocean currents!
Basic animation
import { Animated, Pressable, StyleSheet } from 'react-native';
import { useRef } from 'react';
function FadeInView() {
const fadeAnim = useRef(new Animated.Value(0)).current;
const fadeIn = () => {
Animated.timing(fadeAnim, {
toValue: 1,
duration: 500,
useNativeDriver: true,
}).start();
};
const fadeOut = () => {
Animated.timing(fadeAnim, {
toValue: 0,
duration: 500,
useNativeDriver: true,
}).start();
};
return (
<View>
<Animated.View style={[styles.box, { opacity: fadeAnim }]}>
<Text>Fading element</Text>
</Animated.View>
<Pressable onPress={fadeIn}><Text>Show</Text></Pressable>
<Pressable onPress={fadeOut}><Text>Hide</Text></Pressable>
</View>
);
}Different animation types
// timing - linear animation
Animated.timing(value, { toValue: 1, duration: 300, useNativeDriver: true });
// spring - spring effect
Animated.spring(value, { toValue: 1, friction: 5, tension: 40, useNativeDriver: true });
// decay - deceleration
Animated.decay(value, { velocity: 0.5, deceleration: 0.997, useNativeDriver: true });
// Combining animations
Animated.sequence([anim1, anim2]); // one after another
Animated.parallel([anim1, anim2]); // simultaneously
Animated.stagger(100, [anim1, anim2, anim3]); // with delayInterpolation
const animValue = useRef(new Animated.Value(0)).current;
const rotation = animValue.interpolate({
inputRange: [0, 1],
outputRange: ['0deg', '360deg'],
});
const backgroundColor = animValue.interpolate({
inputRange: [0, 0.5, 1],
outputRange: ['#1a5276', '#00d4ff', '#1a5276'],
});
<Animated.View style={{ transform: [{ rotate: rotation }], backgroundColor }} />Code for this lesson: App.tsx
import React, { useRef } from 'react';
import { Animated, View, Text, Pressable, StyleSheet } from 'react-native';
export default function App() {
// TODO: create an animated value starting at 0
// use useRef so it survives re-renders:
// const fadeAnim = useRef(new Animated.Value(0)).current;
const fadeIn = () => {
// TODO: run Animated.timing on fadeAnim:
// toValue: 1, duration: 500, useNativeDriver: true
// remember to call .start() at the end
};
const fadeOut = () => {
// TODO: run Animated.timing on fadeAnim:
// toValue: 0, duration: 500, useNativeDriver: true, then .start()
};
return (
<View style={styles.container}>
{/* TODO: bind fadeAnim to the opacity style of the animated view:
style={[styles.box, { opacity: fadeAnim }]} */}
<Animated.View style={styles.box}>
<Text style={styles.boxText}>Ocean current</Text>
</Animated.View>
<Pressable style={styles.button} onPress={fadeIn}>
<Text style={styles.buttonText}>Show</Text>
</Pressable>
<Pressable style={styles.button} onPress={fadeOut}>
<Text style={styles.buttonText}>Hide</Text>
</Pressable>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#1a5276',
},
box: {
width: 200,
height: 200,
borderRadius: 16,
backgroundColor: '#00d4ff',
justifyContent: 'center',
alignItems: 'center',
marginBottom: 24,
},
boxText: {
color: '#0a2540',
fontSize: 18,
fontWeight: '700',
},
button: {
paddingVertical: 12,
paddingHorizontal: 28,
borderRadius: 8,
backgroundColor: '#2980b9',
marginVertical: 6,
},
buttonText: {
color: '#fff',
fontSize: 16,
fontWeight: '600',
},
});Docs entries used in this lesson
Spotted a mistake in this lesson?
Check yourself
Answer the questions from this lesson. Pick an answer to see right away whether it is correct.
1. How do you create an animated value?
2. Why do we use useRef with Animated.Value?
These are 2 of 7 questions for this lesson. Solve the rest in the game.
Hands-on tasks in the game
- Click in order
Click the elements of Animated.timing:
- Horizontal ordering
Arrange starting an animation with a callback:
- Vertical ordering
Arrange the interpolation:
- Code editor
Complete the FadeInView code.