An Interest In:
Web News this Week
- April 24, 2024
- April 23, 2024
- April 22, 2024
- April 21, 2024
- April 20, 2024
- April 19, 2024
- April 18, 2024
React Native Reanimated 2 Layout Animation Example
Layout Animations are the easiest way to animate entering, exiting and change layout of your React Native components with Reanimated 2 library.
Looks good, right? And it super easy to implement!
TLDR You can find the source code on reanimated-cards-layout repo.
Setup
First, we need to create a new React Native project. Then we will need to install react-native-reanimated
library and reanimated
babel plugin. Please follow the documentation to do that.
Layout Animation
Imaging we have a "child" Card
component:
const Card = ({ image, title }) => ( <View style={styles.container}> <Image source={image} /> <Text>{title}</Text> </View>)
And a parent component which renders list of cards:
<View style={styles.row}> {cards.map(({ image, title }) => ( <Card image={image} title={title} /> ))}</View>
Now, to be able to animate cards entering, exiting and layout changes, all we need to do is slightly change the Card
component:
- Switch from the
View
toAnimated.View
component. - Add
entering
,exiting
andlayout
props to it.
import Animated, { FadeOutDown, FadeInUp, Layout, Easing } from 'react-native-reanimated'const Card = ({ image, title }) => ( <Animated.View style={styles.container} layout={Layout.duration(200).delay(200)} entering={FadeInUp} exiting={FadeOutDown} > <Image source={image} /> <Text>{title}</Text> </Animated.View>)
That's all!
We can use predefined entering, exiting and layout transitions. Or create a custom animation.
If you have any questions, please post them in comments, press button and happy hacking!
Credits
Photo by Pixabay.
Original Link: https://dev.to/vladimirvovk/react-native-reanimated-2-layout-animations-example-3i0h
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To