This site runs best with JavaScript enabled.
Share

15 React Native libraries you should use in 2021

Youssouf El Azizi

May 10, 2021


Originally published on Logrocket Blog

The React Native community is one of the more vibrant open source communities on the web. According to GitHub, the React Native repository is one of the top-rated repos based on the number of contributors. You can find modules and third-party libraries for almost anything you want to implement in your next mobile application.

You’re bound to encounter issues related to deprecated and unmaintained packages, especially packages from the early days of React Native — it’s simply a fact of life. I think it’s an acceptable burden since maintaining a React Native library requires maintaining a project that can depend on at least three languages.

It is crucial to know which libraries to use to create the best possible user and developer experience. In this tutorial, we’ll introduce you to 15 React Native UI libraries that I’ve used in virtually every React Native application I’ve worked on so far in 2021.

We’ll cover the best React Native libraries for:

Then, we’ll go over some tips , including how to:

lets Go

Styling

Styling is a very opinionated topic in the React community and JavaScript communities in general. I have used almost all CSS-in-JS approaches to style my React and React Native applications. I’ve settled on using Tailwind CSS for web and Restyle for React Native.

Restyle is a library developed by Shopify with a type-enforced system for building UI components. With Restyle, you can make a complete design system by defining colors, spacing, and variations and creating your components based on React Native core components and Restyle utilities.

The best feature of Restyle is that it forces you to use only configurations you already declared in your theme, which keeps the application clean and guides your coworkers by keeping their choices as minimal as possible.

Restyle requires you first to create a theme that reflects your design system config (colors, spacing, breakpoints, and variants), like this:

1import { createTheme } from "@shopify/restyle";
2
3const palette = {
4 purpleLight: "#8C6FF7",
5 purplePrimary: "#5A31F4",
6 purpleDark: "#3F22AB",
7
8 greenLight: "#56DCBA",
9 greenPrimary: "#0ECD9D",
10 greenDark: "#0A906E",
11
12 black: "#0B0B0B",
13 white: "#F0F2F3",
14};
15
16const theme = createTheme({
17 colors: {
18 mainBackground: palette.white,
19 cardPrimaryBackground: palette.purplePrimary,
20 },
21 spacing: {
22 s: 8,
23 m: 16,
24 l: 24,
25 xl: 40,
26 },
27 breakpoints: {
28 phone: 0,
29 tablet: 768,
30 },
31});
32
33export type Theme = typeof theme;
34export default theme;

This what your components will look like using Restyle:

Restyle

Navigation

If you had asked me two years ago, I would’ve cautioned against using React Navigation due to some performance challenges and recommended using a native library, such as React Native Navigation, instead.

Fortunately, the React Navigation community, with some help from Software Mansion, developed and optimized three of the most-used libraries in the React Native ecosystem: React Native Screens, React Native Gesture Handler, and React Native Reanimated. Those three libraries are the secret behind the huge performance improvement you’ll notice starting from version 4.

Today, React Navigation should be your first choice; you don’t need to think twice.

To start working with React Navigation, you must first install all its dependencies:

1yarn add react-native-reanimated react-native-gesture-handler react-native-screens react-native-safe-area-context @react-native-community/masked-view

You can use three common types of navigator with React Navigation: Stack, Tab, and Drawer. You can also combine multiple navigators and create a complex app architecture.

Here’s a simple example of a stack navigator:

1import { createStackNavigator } from "@react-navigation/stack";
2const Stack = createStackNavigator();
3function MyStack() {
4 return (
5 <Stack.Navigator>
6 <Stack.Screen name="Home" component={Home} />
7 <Stack.Screen name="Notifications" component={Notifications} />
8 <Stack.Screen name="Profile" component={Profile} />
9 <Stack.Screen name="Settings" component={Settings} />
10 </Stack.Navigator>
11 );
12}

Splash screens

Adding a splash screen to your mobile application is a tedious task, and the best way to accomplish tedious tasks is to automate them. react-native-bootsplash enables you to create a fancy splash screen using CLI. All you need to do is provide an image and background color and the package will do the work for you.

I prefer using react-native-bootsplash over the most popular package, react-native-splash-screen, because the former prevents you from seeing the red errors if you’re facing an issue on startup and sticks on the splash screen instead, which is annoying.

The following command generates a splash screen in react-native-bootsplash:

1yarn react-native generate-bootsplash assets/bootsplash_logo_original.png \
2 --background-color=F5FCFF \
3 --logo-width=100 \
4 --assets-path=assets \
5 --flavor=main

Splash Screen React Native Bootsplash Display

App icons

This is another tedious chore we need to automate. To automate generating app icons, we’ll use a plugin called React Native Make. The plugin is available through the React Native CLI and is designed to help you generate app icons for iOS and Android platforms.

To use react-native-make, you just need a 1024×1024 version of your app icon and you’re ready to generate app icon assets using the following commands:

1react-native set-icon --path path-to-image

Loading placeholder

One of the most common questions I receive about React Native mobile apps is how to create a loading experience similar to apps such as Facebook and YouTube.

There are multiple solutions to create such an experience, but I would recommend using the react-content-loader package. This package is based on the react-native-svg and Reanimated libraries, which seem to work very smoothly. The package also provides a playground to help you create a placeholder in your browser.

Here’s an example of a loading placeholder similar to that in the Facebook app:

1import React from "react";
2import ContentLoader, { Rect, Circle, Path } from "react-content-loader/native";
3
4const MyLoader = (props) => (
5 <ContentLoader
6 speed={2}
7 width={400}
8 height={460}
9 viewBox="0 0 400 460"
10 backgroundColor="#d1d1d1"
11 foregroundColor="#c4c4c4"
12 {...props}
13 >
14 <Circle cx="31" cy="31" r="15" />
15 <Rect x="58" y="18" rx="2" ry="2" width="140" height="10" />
16 <Rect x="58" y="34" rx="2" ry="2" width="140" height="10" />
17 <Rect x="0" y="60" rx="2" ry="2" width="400" height="400" />
18 </ContentLoader>
19);
20
21export default MyLoader;

Loading Placeholder Example

Handling and tracking errors

We all aim to make our React Native application bug-free by using a typing system and increasing testing coverage. However, even with high testing coverage, users are still sure ti encounter and report bugs.

Therefore, it’s crucial to handle your errors and provide feedback to users whenever the app is not working as expected. react-native-exception-handler offers a simple way to handle native and JavaScript errors and deliver feedback to users.

To make react-native-exception-handler work, you must install and link the module. Next, register your global handler for JavaScript and native exceptions, like so:

1import {
2 setJSExceptionHandler,
3 setNativeExceptionHandler,
4} from "react-native-exception-handler";
5
6setJSExceptionHandler((error, isFatal) => {
7 // This is your custom global error handler
8 // You do stuff like show an error dialog
9 // or hit google analytics to track crashes
10 // or hit a custom api to inform the dev team.
11});
12
13const exceptionhandler = (exceptionString) => {
14 // your exception handler code here
15};
16setNativeExceptionHandler(
17 exceptionhandler,
18 forceAppQuit,
19 executeDefaultHandler
20);

You should track these errors with a third-party tool that notifies you of the errors users encounter in your application so you fix them in future releases.

Fetching data

My suggestion in this section will depend on your back-end implementation. If you are using a REST API, react-query is your best choice here. But instead, if you’re using graphQL, you can use urql. Those two libraries will provide you with everything you will need to handle API in your application. You will benefit from a ton of unique features like caching, offline support, optimistic UI, prefetching, and much more.

After installing react-query you can create your own hook to fetch tasks as example:

1import { useQuery } from "react-query";
2import { client } from "./client";
3
4const getTasks = async () => {
5 const { data } = await client.get("/tasks");
6 return data;
7};
8
9export function useTasks() {
10 return useQuery("tasks", getTasks);
11}

And use it inside your components to fetch tasks :

1export const Tasks = () => {
2 const { isLoading, data } = useTasks();
3
4 if (isLoading) {
5 return <ActivityIndicator color="#000" />;
6 }
7 return (
8 <FlatList
9 ListHeaderComponent={() => <Header />}
10 data={data || []}
11 renderItem={({ item }) => <TaskItem {...item} />}
12 keyExtractor={(_, index) => `item-${index}`}
13 showsHorizontalScrollIndicator={false}
14 />
15 );
16};

Icons

Most articles I’ve seen about using icons in React Native suggest using React Native Vector Icons as a default choice. It’s true that this library has numerous icons and fonts, but more often than not, my team found itself creating new fonts to accompany custom designs in our icons. In my opinion, creating a custom font with react-native-vector-icon makes for a suboptimal experience because you need to generate a new font whenever you want to add a new icon.

Instead, my team start using react-native-svg for our icons with the amazing SVGR package, which can generate a React component from any SVG file. You can even directly export a React component from a Figma file.

Below is a simple example of an SVG icon generated as React Native components using the SVGR Figma plugin:

1import * as React from "react";
2import Svg, { SvgProps, Path } from "react-native-svg";
3/* SVGR has dropped some elements not supported by react-native-svg: title */
4
5function SvgComponent(props: SvgProps) {
6 return (
7 <Svg width={48} height={1} viewBox="0 0 48 1" {...props}>
8 <Path d="M0 0h48v1H0z" fill="#063855" fillRule="evenodd" />
9 </Svg>
10 );
11}
12
13export default SvgComponent;

Images

If your application depends mainly on images, you’ll notice some performance issues, especially with lists and scroll view.

Using FastImage, a performant React Native image component, will help you improve your application without any extra effort. It exactly replaces the image component from React Native and adds some amazing features, such as caching and prioritizing and reloading.

Forms

There are some great solutions out there for handling forms in React Native, including Formik and React Hook Form.

I used to use Formik, but I’ve been hooked on React Hook Form ever since I discovered it. In my opinion, this is the best solution for handling simple and complex forms in React.

Other benefits of using React Hook Form include state management, validation, errors management, and multiple array fields.

Testing

If you’ve already worked with the testing libraries to test your frontend applications, your knowledge and experience will be applicable to testing in React Native. React Native Testing Library has virtually the same API.

Here’s a simple counter unit test using React Native Testing Library:

1import "react-native";
2import React from "react";
3import { fireEvent, render } from "@testing-library/react-native";
4
5it("renders correctly", () => {
6 // render component
7 const { getByText } = render(<Counter />);
8
9 // get buttons and text elements
10 const decrement = getByText(/decrement/i);
11 const increment = getByText(/increment/i);
12 const counterText = getByText(/Current count:/i);
13
14 // Make sure you have the right values
15 expect(counterText.props.children).toEqual(["Current count: ", 0]);
16 // trigger an event
17 fireEvent.press(increment);
18 expect(counterText.props.children).toEqual(["Current count: ", 1]);
19 fireEvent.press(decrement);
20 expect(counterText.props.children).toEqual(["Current count: ", 0]);
21});

To test your application’s behavior with confidence, I would recommend writing end-to-end tests using the Detox library from Wix.

Tips and best practices for using React Native UI libraries

Below are some bonus tips to help you get the most out of the React Native libraries detailed above.

Create a React Native template

If you expect to use all these libraries in your upcoming React Native projects, it might be worth your time to create a simple template so you can easily start a new project.

We recently created a template for our mobile team to start new projects with all the aforementioned libraries. Here it is if you want to use it:

1npx react-native init MyApp --template https://github.com/obytes/react-native-template-obytes

Get updates about new libraries

One of the most effective ways to get updated about new libraries and best practices is to follow open source projects. I like to check the package.json file for every React Native project shared on my network. Whenever I find a new library, I search for it to learn more.

Also, be sure to follow the react-native topic on GitHub explore page. You’ll receive a ton of React Native projects and discussions.

Use TypeScript with React Native

I started using TypeScript as the primary language for React Native projects about a year-and-a-half ago, and I wish I had done so earlier. Using TypeScript helps you improve the quality of your code and the developer experience because it helps you prevent errors while typing and improve autocomplete functionality.

It’s important to note that TypeScript is only a JavaScript superset with optional static typing. It doesn’t require you to learn a whole new language.

Conclusion

If you think I am missing some great libraries that can help me improve my React Native Application please make sure to let me know in the comments.

I hope you found that interesting, informative, and entertaining. I would be more than happy to hear your remarks and thoughts.

If you think other people should read this post. Tweet, share and Follow me on twitter for the next articles.

Share article 👉

Hire Me !

👋 Hey, if you search for a reputable react-native developer with more than four years of building mobile applications to produce top-quality Applications, you are in the right place; Get in touch to discuss your needs now!

How i can help you?

  • Building your next mobile application using react native best practices with performance in mind
  • Provide consulting and code review for your team to help them build clean and mobile preferment application
  • Investigate on application's performance and issues and help you fix them and optimize application performance.


Get updates via email

JavaScript, React, React Native and web thoughts


Youssouf El Azizi © 2021