Mastering Animations in React Native with Reanimated

Animations play a crucial role in enhancing the user experience of mobile applications. In React Native, achieving smooth and performant animations can be challenging, especially with the built-in Animated API. This is where Reanimated comes in, offering improved performance, better gesture handling, and native thread execution. In this blog, we’ll dive into React Native Reanimated, its features, and how you can leverage it to create stunning animations.
Why Use Reanimated?
Reanimated is an advanced animation library that enables developers to create complex animations that run directly on the UI thread, eliminating the performance bottlenecks of the JavaScript thread. Some of its advantages include:
- Smooth animations: Runs animations on the native thread, ensuring better performance.
- Better gesture support: Seamless integration with react-native-gesture-handler.
- Declarative animations: Simplifies animation logic with hooks like useSharedValue and useAnimatedStyle.
- Worklets: Allows JavaScript code to execute synchronously on the UI thread.
Setting Up Reanimated
To get started, install Reanimated in your React Native project:
npm install react-native-reanimated
Then, ensure that Reanimated is properly configured by adding the required Babel plugin in
babel.config.js:
module.exports = {
presets: ['module:metro-react-native-babel-preset'],
plugins: ['react-native-reanimated/plugin'],
};
For iOS, run:
cd ios && pod install
Restart the development server to apply the changes:
npm run start --reset-cache
Creating a Simple Animation with Reanimated
Let’s create a basic animation using Reanimated 2 that moves a box from left to right when a button is pressed.
Step 1: Import Required Modules
import React from 'react';
import { View, Button, StyleSheet } from 'react-native';
import Animated, { useSharedValue, useAnimatedStyle, withSpring } from 'react-native-reanimated';
Step 2: Define the Animated Component
const AnimatedBox = () => {
const translateX = useSharedValue(0);
const animatedStyle = useAnimatedStyle(() => {
return {
transform: [{ translateX: translateX.value }],
};
});
return (
<View style={styles.container}>
<Animated.View style={[styles.box, animatedStyle]} />
<Button title=”Move Right” onPress={() => (translateX.value = withSpring(200))} />
</View>
);
};
Step 3: Define Styles
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
box: {
width: 100,
height: 100,
backgroundColor: 'blue',
},
});
export default AnimatedBox;
Explanation:
- useSharedValue: A mutable value that holds animation state.
- useAnimatedStyle: Returns styles that update reactively.
- withSpring: A function that animates values using a natural spring effect.
- Button Press: Updates translateX.value, triggering the animation.
Advanced Animations: Gestures & Interactions
Beyond simple transitions, Reanimated enables gesture-based animations with react-native-gesture-handler.
Example: Dragging an Animated Box
import { PanGestureHandler } from 'react-native-gesture-handler';
import Animated, { useAnimatedGestureHandler, useSharedValue, useAnimatedStyle, withSpring } from 'react-native-reanimated';
const DraggableBox = () => {
const translateX = useSharedValue(0);
const translateY = useSharedValue(0);
const gestureHandler = useAnimatedGestureHandler({
onStart: (_, ctx) => {
ctx.startX = translateX.value;
ctx.startY = translateY.value;
},
onActive: (event, ctx) => {
translateX.value = ctx.startX + event.translationX;
translateY.value = ctx.startY + event.translationY;
},
onEnd: () => {
translateX.value = withSpring(0);
translateY.value = withSpring(0);
},
});
const animatedStyle = useAnimatedStyle(() => {
return {
transform: [{ translateX: translateX.value }, { translateY: translateY.value }],
};
});
return (
);
};
Conclusion
With React Native Reanimated, you can create smooth, high-performance animations that feel natural and responsive. Whether you’re animating simple UI elements or handling complex gestures, Reanimated offers a powerful and declarative way to achieve your goals. Start experimenting today and take your animations to the next level!
Recommended Posts

Understanding Threads in React Native
January 9, 2025

State Management in React: Context API vs Redux
January 2, 2025