React Native course · Module 10: Animations

react-native-reanimated - Advanced animations

1 min read ·
In this lesson4

Reanimated is a more powerful animation library!

Installation

npx expo install react-native-reanimated

babel.config.js configuration

module.exports = {
  presets: ['babel-preset-expo'],
  plugins: ['react-native-reanimated/plugin'], // must be last!
};

Reanimated basics

import Animated, {
  useSharedValue,
  useAnimatedStyle,
  withSpring,
  withTiming,
} from 'react-native-reanimated';

function SpringBox() {
  const offset = useSharedValue(0);

  const animatedStyles = useAnimatedStyle(() => ({
    transform: [{ translateX: offset.value }],
  }));

  const moveRight = () => {
    offset.value = withSpring(200);
  };

  const moveLeft = () => {
    offset.value = withSpring(0);
  };

  return (
    <View>
      <Animated.View style={[styles.box, animatedStyles]} />
      <Pressable onPress={moveRight}><Text>Right</Text></Pressable>
      <Pressable onPress={moveLeft}><Text>Left</Text></Pressable>
    </View>
  );
}

Gestures with Gesture Handler

import { Gesture, GestureDetector } from 'react-native-gesture-handler';
import Animated, { useSharedValue, useAnimatedStyle, withSpring } from 'react-native-reanimated';

function DraggableBox() {
  const translateX = useSharedValue(0);
  const translateY = useSharedValue(0);

  const panGesture = Gesture.Pan()
    .onUpdate((event) => {
      translateX.value = event.translationX;
      translateY.value = event.translationY;
    })
    .onEnd(() => {
      translateX.value = withSpring(0);
      translateY.value = withSpring(0);
    });

  const animatedStyle = useAnimatedStyle(() => ({
    transform: [
      { translateX: translateX.value },
      { translateY: translateY.value },
    ],
  }));

  return (
    <GestureDetector gesture={panGesture}>
      <Animated.View style={[styles.box, animatedStyle]} />
    </GestureDetector>
  );
}
Code for this lesson: App.tsx
import React from 'react';
import { View, Text, Pressable, StyleSheet } from 'react-native';
import Animated, {
  useSharedValue,
  useAnimatedStyle,
  withSpring,
} from 'react-native-reanimated';

export default function App() {
  // TODO: create a shared value for the horizontal offset (start: 0)
  //       const offset = useSharedValue(0);

  // TODO: build an animated style that shifts the view by offset.value:
  //       const animatedStyles = useAnimatedStyle(() => ({
  //         transform: [{ translateX: offset.value }],
  //       }));

  const moveRight = () => {
    // TODO: assign offset.value = withSpring(120);

  };

  const moveLeft = () => {
    // TODO: assign offset.value = withSpring(0);

  };

  return (
    <View style={styles.container}>
      {/* TODO: attach animatedStyles to the box style:
          style={[styles.box, animatedStyles]} */}
      <Animated.View style={styles.box} />

      <View style={styles.row}>
        <Pressable style={styles.button} onPress={moveLeft}>
          <Text style={styles.buttonText}>Left</Text>
        </Pressable>
        <Pressable style={styles.button} onPress={moveRight}>
          <Text style={styles.buttonText}>Right</Text>
        </Pressable>
      </View>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#1a5276',
  },
  box: {
    width: 90,
    height: 90,
    borderRadius: 12,
    backgroundColor: '#00d4ff',
    marginBottom: 32,
  },
  row: {
    flexDirection: 'row',
    gap: 12,
  },
  button: {
    paddingVertical: 12,
    paddingHorizontal: 24,
    borderRadius: 8,
    backgroundColor: '#2980b9',
  },
  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. 1. How do you install react-native-reanimated?

  2. 2. Where must the reanimated plugin be in babel.config.js?

These are 2 of 9 questions for this lesson. Solve the rest in the game.

Hands-on tasks in the game

  • Horizontal ordering

    Arrange useAnimatedStyle:

  • Click in order

    Click the elements of withTiming:

  • Code editor

    Complete the code using withSpring.

  1. QuestionInstalling Reanimated
  2. Questionbabel.config.js
  3. QuestionuseSharedValue
  4. QuestionModifying .value
  5. Put in orderuseAnimatedStyle
  6. QuestionwithSpring
  7. Click in orderwithTiming config
  8. Coding taskSpring animation
  9. QuestionGesture.Pan
  10. QuestionGestureDetector
  11. QuestiononUpdate
  12. QuestionrunOnJS

Useful articles