How to structure a react-native project

Youssouf EL Azizi,Β 
November 17th, 2020 Β· 3 min read

As I have been writing tutorials about react native in the last couple of months, people usually ask me about the best way to structure a react native project to make it clean, scalable, and easy to follow for newcomers developers.

In the following post, I will share an easy, clean, and scalable project structure ready to use for your next React Native project.

I need to mention that the approach reflects how we use React native in our Obytes mobile team.

Don’t miss the bonus part at the end 😎


lets Go

Before I start talking about project structure, I would love to share some essential ideas you should consider for a good react native project structure.

Use Typescript

I start working with typescript for react-native a year ago. I would see that using typescript is super useful to help write clean and secure code. Still, it’s a little bit confusing in the first days as I found myself dealing with typing more than application logic, but after that, I can’t see myself creating a new project without typescript. So make sure to give a try and check those amazing Cheatsheets for experienced React developers getting started with TypeScript; you can thank me later :)

Setup Husky pre-commit hook with ESLint

Husky lets us run commands or scripts before committing or pushing our code to git. It’s beneficial to set up some pre-commit hook easily. We usually use it to run Eslint and Prettier to validate changed files typing before commit.

Install deps :

1yarn add husky lint-staged

Add the following config to the package.json file.

1// package.json
2"husky": {
3 "hooks": {
4 "pre-commit": "lint-staged"
5 }
6 },
7 "lint-staged": {
8 "*.{js,jsx,tsx}": "eslint"
9 }

Setup module-resolver for easy import

During your journey of writing clean and reusable code, for sure, you are going to use import x for "file" statement, and as you code source grow, you will find yourself writing import some wired import statement like the following:

1import foo from '../.../../folder/foo'

The best way to make your code clean in such cases is to use absolute import. To start using absolute import in your project, you need to update baseUrl config inside the tsconfig.json configuration file to ./src. Aso you will need to use the module-resolver babel plugin to update the resolver root folder for babel like the following:

1// babel.config.js
2
3module.exports = {
4 presets: ['module:metro-react-native-babel-preset'],
5 plugins: [
6 'module:react-native-dotenv',
7 [
8 'module-resolver',
9 {
10 root: ['./src'],
11 extensions: [
12 '.ios.ts',
13 '.android.ts',
14 '.ts',
15 '.ios.tsx',
16 '.android.tsx',
17 '.tsx',
18 '.jsx',
19 '.js',
20 '.json',
21 ],
22 },
23 ],
24 ],
25}

As we agree on those points we can jump now to how to structure your react native project.

Project structure

The is a real folder tree of a React Native application src folder i recommend to follow:

1.
2β”œβ”€β”€ index.tsx
3β”œβ”€β”€ api
4β”‚ β”œβ”€β”€ usePosts.tsx
5β”‚ └── index.tsx
6β”œβ”€β”€ core
7β”‚ β”œβ”€β”€ Auth
8β”‚ β”œβ”€β”€ I18n
9β”‚ └── index.tsx
10β”œβ”€β”€ navigation
11β”‚ β”œβ”€β”€ AuthNavigator.tsx
12β”‚ β”œβ”€β”€ NavigationContainer.tsx
13β”‚ β”œβ”€β”€ RootNavigator.tsx
14β”‚ β”œβ”€β”€ TabNavigator.tsx
15β”‚ └── index.tsx
16β”œβ”€β”€ screens
17β”‚ β”œβ”€β”€ Home
18β”‚ β”œβ”€β”€ Login
19β”‚ └── index.tsx
20β”œβ”€β”€ translations
21β”‚ └── en.json
22β”‚ └── ar.json
23└── ui
24 β”œβ”€β”€ Button.tsx
25 β”œβ”€β”€ ErrorHandler
26 β”œβ”€β”€ Form.tsx
27 β”œβ”€β”€ Icon.tsx
28 β”œβ”€β”€ Input.tsx
29 β”œβ”€β”€ Screen.tsx
30 β”œβ”€β”€ Text.tsx
31 β”œβ”€β”€ Toast.tsx
32 β”œβ”€β”€ View.tsx
33 β”œβ”€β”€ index.tsx
34 └── theme

You can think of this as modules for our mobile app and every module include an application related files :

  • ui : in the ui folder we create our design system and all common components and theme configuration based on restyle and react-native-svg for icons.
  • core: this folder is useful for any Provider and general concepts( a folder that you can easily copy-paste between projects). here, we put our Auth, localization…
  • translations: translations files
  • navigation: all navigation-related components ( Stacks, Tabs …)
  • api : this folder will contain all api related hooks created using apollo-client or react-query depend on the backend implementation.
  • screens: a folder for applications screens, we usually create a new folder for every screen or application module, and for every screen folder, you should see a components folder where we create screen components.

A little trick that deserves to be mentioned here is that every module above will have a root index file that exports all components and functions from the entire folder tree, which makes importing components very clean.

Example of ui index folder :

1//ui/index.js
2export * from './theme'
3export * from './View'
4export * from './Text'
5export * from './Button'
6export * from './Screen'
7export * from './Input'
8export * from './Form'
9export * from './ErrorHandler'
10export * from './icons'

Example of a screen Home:

1// screens/Home/index.tsx
2import React from 'react'
3import { Button, Screen, Text, View } from 'ui'
4import { API } from '@env'
5import { translate, useAuth } from 'core'
6
7export const Home = () => {
8 const { signOut } = useAuth()
9 return (
10 <Screen>
11 <View flex={1} justifyContent="center">
12 <Text variant="header" textAlign="center">
13 {translate('name')}
14 </Text>
15 <Text variant="body" textAlign="center">
16 This is An ENV Var : {API}
17 </Text>
18 <Button label="LogOut" onPress={signOut} />
19 </View>
20 </Screen>
21 )
22}

As you can see all ideas we discuss in this post help us create clean screen components.

Bonus

What if i told you that you can benefit from those tricks and project structure ideas using a command-line tool, Well, recently we create an Obytes react native template based on Obytes mobile tribe best practices and come with the following features :

βœ… Typescript by default based on official Typescript template
βœ… Generate App Icon and Splash screen
βœ… React Navigation Pre-installed
βœ… Auth flow with sensitive-info to secure tokens
βœ… A clean project structure based on this blog post.
βœ… Minimal UI kit setup using restyle and configurable theme & icons using react-native-svg
βœ… A good approach to handle forms based on react-hook-form
βœ… A complete setup to Handle Errors
βœ… Handel environment variables with react-native-env
βœ… Localization

And you can use it as a template for react-native cli.

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

Make sure to give it a try and help us improve the template.

I hope you found that interesting, informative, and entertaining. I would be more than happy to hear your remarks and thoughts about this solution in The comments.

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

More articles from Obytes

Deploying Boomi on AWS ECS

How to deploy a dockerized Boomi infrastructure on AWS ECS

November 12th, 2020 Β· 4 min read

Setting up a Cloudflare Argo Tunnel on AWS Fargate.

A complete guide to set up a Cloudflare Argo Tunnel on AWS Fargate.

November 8th, 2020 Β· 2 min read

ABOUT US

Our mission and ambition is to challenge the status quo, by doing things differently we nurture our love for craft and technology allowing us to create the unexpected.