In this tutorial, we'll explore how to create an engaging skeleton loading animation in a React Native application using the Moti library.
Moti is a powerful animation library built on top of Reanimated, providing a straightforward way to add complex animations to your React Native projects.
J
John Doe
john.doe@example.com
Source Code
You can find the source code on GitHub:
YouTube Channel
Are you more of a visual learner? Here you can find my full video tutorial
Project Setup
To get started, we'll create an Expo Go project with TypeScript:
npx create-expo-app --template blank-typescript
The goal will be to focus on the Skeleton Loader animation, but of course we'll need a simple placeholder component to display the animation.
To keep things simple, we'll create a ContactListItem component that will display the Skeleton Loader if the contact is null.
You can easily display the placeholder component by passing null as the contact prop.
<InteractiveContactItem
contact={{
name:'John Doe',
email:'john.doe@example.com',
}}
/>
This should be the current state:
J
John Doe
john.doe@example.com
The Skeleton Loader
Wait, where's our skeleton loader?
To create it, we'll use the Moti library. I'm a huge fan of creating things from scratch, but in this specific case, there's no need to reinvent the wheel.
npx expo install moti
This is not enough. You can easily understand why by asking yourself: What would I need to do if I wanted to create a Skeleton loader?
A Linear Gradient to create the effect -> expo-linear-gradient
A way to animate the gradient -> react-native-reanimated
Note: if you're not using Expo, you can use react-native-linear-gradient instead of expo-linear-gradient (but why aren't you using Expo?!)
Now that we have both our ContactListItem and Skeleton components, let's combine them to handle loading states elegantly. We'll modify our ContactListItem to show the skeleton when the contact data is loading:
Try clicking the play button above to see the skeleton loader in action!
J
John Doe
john.doe@example.com
Conclusion
Skeleton loaders are an excellent way to improve the perceived performance of your React Native applications. Using Moti makes implementing these animations straightforward and maintainable. Remember these key takeaways:
Use skeleton loaders to indicate that content is loading
Leverage Moti's built-in skeleton components for easy implementation
Handle loading states by showing skeletons: here we used a simple null check, but usually it would be better to use a proper loading state.
Add smooth transitions between loading and loaded states using animations
The combination of Moti, Reanimated, and Linear Gradient provides a powerful toolkit for creating beautiful loading experiences in your React Native applications.
My newsletter
Sometimes I enjoy sending out a newsletter sharing new things about React Native animations.