I started creating YouTube tutorials in 2021, and everything started with this video: "Introduction to Reanimated 2". If you're curious, here's the written article.
Since then, Reanimated is still the best way to animate in React Native and the content explained in the video is still valid.
First of all, let's make a premise. The code above is still valid and great, sometimes you need to have a lot of control over the animation you're building and the ingredients mentioned above are most likely the best way to do it.
However, here we just wanted to build an animated square. There must be a better way to do it.
Starting from Reanimated 4, we can now pass the initial and final state of the animation within the Animated.View component.
Here's how it works:
constInitialState={
borderRadius:0,
opacity:0.5,
transform:[
{ rotate:'0deg'},
{
scale:0.5,
},
],
}asconst;
constFinalState={
borderRadius:20,
opacity:1,
transform:[
{ rotate:'360deg'},
{
scale:1,
},
],
}asconst;
exportconstAnimatedSquare=()=>{
return(
<Viewstyle={styles.container}>
<Animated.View
style={[
styles.square,
{
animationDuration:2000,
animationTimingFunction:'easeInOut',// use the curve you'd like the most
animationName:{
from:InitialState,
to:FinalState,
},
},
]}
/>
</View>
);
};
Doesn't it look cleaner?
Repeating the animation
So how do we repeat the animation 🧐
Since we're passing the initial and final state of the animation, we can't use the withRepeat high-order function.
Again, there must be a better way to do it.
...// InitialState and FinalState
exportconstAnimatedSquare=()=>{
return(
<Viewstyle={styles.container}>
<Animated.View
style={[
styles.square,
{
animationDuration:2000,
animationTimingFunction:'easeInOut',
animationIterationCount:'infinite',// repeat the animation infinitely (or just 1, 2, 3, etc...)
animationDirection:'alternate',// alternate the animation direction to create a boomerang effect
animationName:{
from:InitialState,
to:FinalState,
},
},
]}
/>
</View>
);
};
Here's how it looks with the animation repeating infinitely:
Playing/Pausing the animation
Pausing the animation is now as easy as setting the animationPlayState property to paused.
The introduction of CSS Animations and Transitions in Reanimated 4 brings several advantages:
Simpler Syntax: Define animations using familiar CSS-like properties instead of complex animation worklets
Declarative Approach: Specify the initial and final states directly in your component's style
Built-in Controls: Easy-to-use animation controls like animationPlayState, animationIterationCount, and animationDirection
Reduced Boilerplate: No need for multiple useSharedValue and useAnimatedStyle hooks for basic animations
Better Developer Experience: More intuitive API that feels closer to web development
While the traditional Reanimated API is still valuable for complex animations and precise control, the new CSS animations provide a more straightforward approach for common use cases.
This addition makes React Native animations more accessible to developers coming from web development and greatly simplifies the implementation of basic animations.
My newsletter
Sometimes I enjoy sending out a newsletter sharing new things about React Native animations.