# react-native-reanimated ### Create Smooth Animations with an Excellent Developer Experience #### Why Choose Reanimated? ##### Declarative API Reanimated offers a declarative approach to creating animations, simplifying complexity from numerous methods down to just a few. Define the desired animation outcome and let Reanimated handle the style and property animations for you. ##### High Performance With Reanimated, define animations in plain JavaScript that run natively on the UI thread by default. This ensures smooth interactions up to 120 fps or more, delivering a native experience users expect. ##### Rich Features Reanimated's capabilities extend beyond animating simple views or images. Integrate your animations with device sensors or keyboards, utilize Layout Animations, and effortlessly animate elements between navigation screens. - **Learn More**: Discover the features of Reanimated 3 in our latest article. - [See blog post](#) ##### Animation Capabilities Animate every React Native prop on iOS, Android, and the Web up to 120 fps. ```typescript function App() { const width = useSharedValue(100); const handlePress = () => { width.value = withSpring(width.value + 50); }; return ; } ``` ##### Gesture Integration Experience smooth gestures through Reanimated's integration with React Native Gesture Handler. ```typescript import { Gesture, GestureDetector } from "react-native-gesture-handler"; function App() { const pan = Gesture.Pan(); return ( ); } ``` ##### Layout Animations Automatically animate views when they are added or removed from the view hierarchy. ```typescript function App() { return ; } ``` ##### Sensor-Based Animations Easily connect your animations to a gyroscope or accelerometer with just one hook. ```typescript const gyroscope = useAnimatedSensor(SensorType.GYROSCOPE) useDerivedValue(() => { const { x, y, z } = gyroscope.sensor.value }) ``` ##### Keyboard-Based Animations Create animations based on the device keyboard state and position. ```typescript function App() { const keyboard = useAnimatedKeyboard() const animatedStyle = useAnimatedStyle(() => ({ transform: [{ translateY: -keyboard.height.value }], })) //... } ``` ##### Shared Element Transitions Seamlessly animate elements between navigation screens with a single line of code. ```typescript function App() { return ; } ``` #### Sponsors ### We are Software Mansion React Native Core Contributors and experts in addressing all types of React Native issues. Whether you need assistance with gestures, animations, or general React Native development, we're here to help. ## useAnimatedStyle `useAnimatedStyle` is a hook that allows you to create an animated styles object, similar to `StyleSheet`, which can be dynamically updated using shared values. These styles must be applied to the `style` property of an Animated component and are automatically refreshed when associated shared values or React state change. Unlike inline styling, `useAnimatedStyle` provides access to values stored in shared values within the defined style object. For animating properties directly on components, consider using `useAnimatedProps`. ### Reference ```typescript import { useAnimatedStyle } from 'react-native-reanimated'; function App() { const animatedStyles = useAnimatedStyle(() => ({ opacity: sv.value ? 1 : 0, })); return ; } ``` #### Arguments ##### `updater` A function that returns an object containing the style properties you wish to animate. Any style property available in React Native can be animated. ##### `dependencies` (Optional) An optional array of dependencies. This is only relevant when using Reanimated without its Babel plugin on the Web. ### Returns The hook returns an animated style object, which should be assigned to the `style` property of an Animated component you wish to animate. The `updater` callback provides a value resembling a regular style object that can incorporate shared values. ### Example #### Remarks - Animated styles override React Native's static styles. Values in animated styles take precedence over those from static styles. ```typescript function App() { const animatedStyles = useAnimatedStyle(() => ({ width: sv.value, })); return ( ); } ``` - The order of animated styles in a style array does not affect their application. The most recently updated animated style is applied. - Removing an animated style from a view does not revert values set by that style. To reset these values, explicitly set them to `undefined` within the animated style. - Modifying shared values inside the `useAnimatedStyle` callback can lead to undefined behavior and potential infinite loops. ```typescript function App() { const sv = useSharedValue(0) const animatedStyles = useAnimatedStyle(() => { sv.value = withTiming(1) // Avoid this! return { opacity: sv.value } }) } ``` - Animated styles should only be applied to `Animated` components. Applying them to non-animated components will cause an error. - Define dynamic styles using `useAnimatedStyle`, while keeping static styles separate via the `StyleSheet` API or inline styles. This minimizes unnecessary style recalculations. Static and dynamic styles can be merged using array syntax: ```typescript function App() { const animatedStyles = useAnimatedStyle(() => ({ offset: sv.value, })); return ; } const styles = StyleSheet.create({ box: { height: 120, width: 120, backgroundColor: '#b58df1', }, }); ``` - Animated styles can be shared between components to reduce code duplication. - The callback for `useAnimatedStyle` executes on both the JavaScript and UI threads. To ensure thread-specific behavior, use the `global._WORKLET` variable: ```typescript function App() { const animatedStyles = useAnimatedStyle(() => { if (global._WORKLET) { // Code specific to the UI thread } else { // Fallback code for the JS thread } }) } ``` ### Platform Compatibility |Android|iOS|Web| |-|-|-| |✅|✅|✅| ## Shared Element Transitions > **Caution:**\ > Shared Element Transition is an experimental feature and not recommended for production use yet. Feedback is welcomed to enhance its implementation. Shared Element Transition enables a smooth transformation of a component from one screen to another. ### Reference ```typescript import Animated from 'react-native-reanimated'; const Stack = createNativeStackNavigator(); function One({ navigation }) { return ( <>