10680b787SAman Mittal---
20680b787SAman Mittaltitle: Animation
30680b787SAman Mittaldescription: Learn how to integrate the react-native-reanimated library and use it in your Expo project.
40680b787SAman Mittal---
50680b787SAman Mittal
60680b787SAman Mittalimport { Terminal, SnackInline } from '~/ui/components/Snippet';
70680b787SAman Mittalimport { BoxLink } from '~/ui/components/BoxLink';
8fa939da8SAman Mittalimport { BookOpen02Icon } from '@expo/styleguide-icons';
90680b787SAman Mittal
100680b787SAman MittalAnimations are a great way to enhance and provide a better user experience. In your Expo projects, you can use the [Animated API](https://reactnative.dev/docs/next/animations) from React Native. However, if you want to use more advanced animations with better performance, you can use the [`react-native-reanimated`](https://docs.swmansion.com/react-native-reanimated/) library. It provides an API that simplifies the process of creating smooth, powerful, and maintainable animations.
110680b787SAman Mittal
120680b787SAman Mittal## Installation
130680b787SAman Mittal
140680b787SAman MittalTo install `react-native-reanimated`, run the following command:
150680b787SAman Mittal
160680b787SAman Mittal<Terminal cmd={['$ npx expo install react-native-reanimated']} />
170680b787SAman Mittal
180680b787SAman MittalAfter the installation completes, add the Babel plugin to **babel.config.js**:
190680b787SAman Mittal
200680b787SAman Mittal```js babel.config.js
210680b787SAman Mittalmodule.exports = function (api) {
220680b787SAman Mittal  api.cache(true);
230680b787SAman Mittal  return {
240680b787SAman Mittal    presets: ['babel-preset-expo'],
250680b787SAman Mittal    plugins: ['react-native-reanimated/plugin'],
260680b787SAman Mittal  };
270680b787SAman Mittal};
280680b787SAman Mittal```
290680b787SAman Mittal
300680b787SAman MittalAfter you add the Babel plugin, restart your development server and clear the bundler cache using `npx expo start --clear`.
310680b787SAman Mittal
320680b787SAman Mittal> If you load other Babel plugins, the Reanimated plugin has to be the last item in the plugins array.
330680b787SAman Mittal
340680b787SAman Mittal### Web support
350680b787SAman Mittal
360680b787SAman Mittal**For web**, install the [`@babel/plugin-proposal-export-namespace-from`](https://babeljs.io/docs/en/babel-plugin-proposal-export-namespace-from#installation) Babel plugin and update the **babel.config.js** to load it:
370680b787SAman Mittal
380680b787SAman Mittal{/* prettier-ignore */}
390680b787SAman Mittal```js babel.config.js
400680b787SAman Mittalplugins: [
410680b787SAman Mittal  '@babel/plugin-proposal-export-namespace-from',
420680b787SAman Mittal  'react-native-reanimated/plugin',
430680b787SAman Mittal],
440680b787SAman Mittal```
450680b787SAman Mittal
460680b787SAman MittalAfter you add the Babel plugin, restart your development server and clear the bundler cache using `npx expo start --clear`.
470680b787SAman Mittal
480680b787SAman Mittal## Minimal example
490680b787SAman Mittal
50*72bb203aSDavid LeulietteThe following example shows how to use the `react-native-reanimated` library to create a simple animation. For more information on the API and its usage, see [**`react-native-reanimated` documentation**](https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/your-first-animation).
510680b787SAman Mittal
520680b787SAman Mittal<SnackInline label="Using react-native-reanimated" dependencies={['react-native-reanimated']}>
530680b787SAman Mittal
540680b787SAman Mittal{/* prettier-ignore */}
550680b787SAman Mittal```jsx
560680b787SAman Mittalimport Animated, {
570680b787SAman Mittal  useSharedValue,
580680b787SAman Mittal  withTiming,
590680b787SAman Mittal  useAnimatedStyle,
600680b787SAman Mittal  Easing,
610680b787SAman Mittal} from 'react-native-reanimated';
620680b787SAman Mittalimport { View, Button, StyleSheet } from 'react-native';
630680b787SAman Mittal
640680b787SAman Mittalexport default function AnimatedStyleUpdateExample() {
650680b787SAman Mittal  const randomWidth = useSharedValue(10);
660680b787SAman Mittal
670680b787SAman Mittal  const config = {
680680b787SAman Mittal    duration: 500,
690680b787SAman Mittal    easing: Easing.bezier(0.5, 0.01, 0, 1),
700680b787SAman Mittal  };
710680b787SAman Mittal
720680b787SAman Mittal  const style = useAnimatedStyle(() => {
730680b787SAman Mittal    return {
740680b787SAman Mittal      width: withTiming(randomWidth.value, config),
750680b787SAman Mittal    };
760680b787SAman Mittal  });
770680b787SAman Mittal
780680b787SAman Mittal  return (
790680b787SAman Mittal    <View style={styles.container}>
800680b787SAman Mittal      <Animated.View style={[styles.box, style]} />
810680b787SAman Mittal      <Button
820680b787SAman Mittal        title="toggle"
830680b787SAman Mittal        onPress={() => {
840680b787SAman Mittal          randomWidth.value = Math.random() * 350;
850680b787SAman Mittal        }}
860680b787SAman Mittal      />
870680b787SAman Mittal    </View>
880680b787SAman Mittal  );
890680b787SAman Mittal}
900680b787SAman Mittal
910680b787SAman Mittalconst styles = StyleSheet.create({
920680b787SAman Mittal  container: {
930680b787SAman Mittal    flex: 1,
940680b787SAman Mittal    alignItems: 'center',
950680b787SAman Mittal    justifyContent: 'center',
960680b787SAman Mittal    flexDirection: 'column',
970680b787SAman Mittal  },
980680b787SAman Mittal  box: {
990680b787SAman Mittal    width: 100,
1000680b787SAman Mittal    height: 80,
1010680b787SAman Mittal    backgroundColor: 'black',
1020680b787SAman Mittal    margin: 30,
1030680b787SAman Mittal  },
1040680b787SAman Mittal});
1050680b787SAman Mittal```
1060680b787SAman Mittal
1070680b787SAman Mittal</SnackInline>
1080680b787SAman Mittal
1090680b787SAman Mittal### Other animation libraries
1100680b787SAman Mittal
1110680b787SAman MittalOther animation packages are available, such as Moti, that you also use in your Expo project and work on Android, iOS, and the web. For more information on its API and usage, see [Moti's documentation](https://moti.fyi/).
1120680b787SAman Mittal
1130680b787SAman Mittal## Next step
1140680b787SAman Mittal
1150680b787SAman Mittal<BoxLink
1160680b787SAman Mittal  title="Store data"
1170680b787SAman Mittal  description="Learn about different libraries available to store data in your app."
1180680b787SAman Mittal  href="/develop/user-interface/store-data"
119fa939da8SAman Mittal  Icon={BookOpen02Icon}
1200680b787SAman Mittal/>
121