Categories
React Native

React Native project structure : A handsome way

React native development doesn’t have any fixed project structure like native ones as it’s based on javascript running on the background. A good project structure keeps project clean, easy to understand for members if you are working in a team. Moreover a good practice of organizing keeps you connected with development for long time. In my time I didn’t find any helpful article/reference and moved on focusing on business logic. But due to lack of proper organization of code I had to face many problems followed by team workers.

In this article I am going to show you a simple but convenient structure that we can use for general purpose application development using react native. This is a fruit of my last 2 years experience on react native and how I start a new project now. We are going to talk about starting and organizing a react native project when done from scratch. Also I will provide Github link of sample project structure at the end of the article.

CREATE NEW PROJECT

We will use react native cli here as I prefer it more than expo but the way we will organize can be used for the both. Also I have yarn installed in my device running ubuntu 18.04. I assume you have react-native installed and you know how to run a project. If not please check this facebook link.

Firstly We will create a simple project that will consist of a landing page, a home screen and a profile page. So lets start our new project like

After creating the project the structure will look like

CREATING REACT NATIVE PROJECT STRUCTURE

Next we will create a src folder and it’s children in the project folder like below.

Easy enough! Huh !! Don’t worry! Github link for the structure is given at the end of the tutorial. Now let me explain the purpose of the structure

We have 6 folders here – assets, components, navigations, screens, services, styles and a file index.js. So let’s see what are they doing here

  • assets – contains static resources like fonts, images etc. we will use in the project.
  • components – contains granule or compound level items here like atoms(for smallest components like custom button, input etc) or mixed of small components if needed.
  • navigations – contains one or more navigation rules for the whole project.
  • scenes – consists of screens or the UI that user will interact with.
  • services – services like APIs or others that is required for the project is placed here. You can also add an utils folder to keep all your utilities.
  • styles – all the stylesheets or colours or typographies required to beautify the application is stored here
  • index.js – the start point where we will define our App that brings all modules together here to link and start navigation of the application.

RESOLVING MODULE

Now we will add “babel-plugin-module-resolver” that simplifies import paths by creating directory alias when compiling your code. This plugin allows you to add new “root” directories that contain your modules. It also allows you to setup a custom alias for directories, specific files, or even other npm modules.

So, we have added module resolver dependency. Now create a .bablerc in our project root folder where we will keep our directory aliases like below

Now we can access above directories with their aliases like ‘_scenes/landing’ etc. Likewise we also need to edit our .eslintrc.js in project root folder to avoid lint errors for new aliases

Let’s Design

Structure is created. Now we will do some minimal design(just a text in each screen) to distinguish each screens. Copy the following snippets for landing/index.js, home/index.js, profile/index.js to your code.

Let’s Navigate

Minimal design is completed. Next, we need to add more dependencies for navigation purpose.

We have added dependencies for navigation, gesture handler, navigation animation, tab navigation. Now let’s add some navigation rules between the screens. Snippets for navigations/land-navigator.js, navigations/app-navigator.js, and navigations/index.js

Above is the App navigator that contains a tab for Home screen and Profile screen.This screen comes after landing page. Next we will create another navigator that will control flow from landing to Home page.

After all navigators are added let’s write our navigations/index.js that comprises of all the navigators and defines RootNavigator.

Now let’s edit our src/index.js to create App

After editing this we can now remove App.js in the project root folder. But before that we need to point to our src directory for start point. So let’s edit our project root folder’s index.js like below.

All set. Now remove the App.js from project’s root folder and run the application.

First start an metro react server on other terminal in project folder

To run in Android, issue the following command from project’s root folder.

And to run in ios use command below

It is working right! This is a basic project structure. You can use it for almost all general kind of apps. However, You can add as much folder as you need. Don’t forget to add new folder paths to .babelrc and .eslintrc.js to access via aliases.

Download

Here is the Github Link for source code. You can download and copy the src folder to your project directly and run.

Reference : An amazing article by Helder Burato

So by now you are familiar with a decent React Native project structure. In conclusion I would like to say If you like this article share it with your friends. Also I would love to hear comments from you.

By Abhishek Pachal

Abhishek is a developer cum blogger working more than 6 years. He loves programming especially open stack technologies. He has decent knowledge in Android development, Wordpress, MongoDB, Node.js and so on. Beside this Abhishek finds himself busy in painting, front-end designing.