This Article Part of React Native Tutorial Series want to start from scrach follow below link

https://infinitbility.com/react-native/table-of-contents

Today, we setup the react native app with an example of project folder structure, React Navigation, and Axios also you guys use for the starter.

Let’s discuss a structure to start new projects or when you need to scale large projects.

We will use React Native as a basis for this architecture, but the concepts can be leveraged in projects using other frameworks.

For the purpose of this post, I will use the following patterns and packages:

– React Navigation: Routing and navigation for your React Native apps;

– Axios: Promise-based HTTP client for the browser and node.js.

Follow the required installation steps on Getting Started · React Native. After configuring the React Native CLI on your machine, verify if react-native -v is available on your terminal.

You should get a return similar to this:

$ react-native-cli: 2.0.1

So we can proceed to create our project.


Creating a project from Scratch

Choose a directory of your choice to create our base project using the following command:

$ react-native init ReactNativeExample

After the execution, you can access the directory using the cd ReactNativeExample. You will get a structure similar to this:

.
├── __tests__
│   ├── App-test.js
├── android
├── ios
├── node_modules
├── .buckconfig
├── .eslintrc.js
├── .flowconfig
├── .gitattributes
├── .gitignore
├── .prettierrc.js
├── .watchmanconfig
├── App.js
├── app.json
├── babel.config.js
├── index.js
├── metro.config.js
├── package.json
└── yarn.lock

Structuring the pillars

We will define a structure for our project to grow efficiently and make its maintenance easier.

Our first step will be to define the directory structure within src (Source). This directory will contain all major project files.

Let’s create the following initial structure for our project:


├── src
│   ├── assets
│   │  ├── fonts
│   │  ├── images
│   ├── components
│   │  ├── atoms
│   │  ├── molecules
│   │  ├── organisms
│   ├── navigations
│   ├── scenes
│   ├── styles
│   ├── utils
│   ├── index.js

Right now you might be wondering why there are so many folders and files, but don’t worry, further on the post we’ll go over their purpose and how important each one of them is.

Enabling the use of the alias

To simplify the require/import of paths in our project, we must configure directory aliases. So let’s install the following packages:


$ yarn add -D eslint-import-resolver-babel-module@^5.1.0
eslint-plugin-import@^2.18.2 babel-plugin-module-resolver@^3.2.0

After installing the dependencies, let’s configure the .babelrc:

.babelrc


{
  "plugins": [
    [
      "module-resolver",
      {
        "cwd": "babelrc",
        "root": ["./src"],
        "extensions": [".js", ".ios.js", ".android.js"],
        "alias": {
          "_assets": "./src/assets",
          "_components": "./src/components",
          "_atoms": "./src/components/atoms",
          "_molecules": "./src/components/molecules",
          "_organisms": "./src/components/organisms",
          "_navigations": "./src/navigations",
          "_scenes": "./src/scenes",
          "_services": "./src/services",
          "_styles": "./src/styles",
          "_utils": "./src/utils"
        }
      }
    ]
  ]
}

Edit the .eslintrc.js file to avoid lint errors when using the new alias:


module.exports = {
  root: true,
  extends: '@react-native-community',
  plugins: ['import'],
  settings: {
    'import/resolver': {
      node: {
        paths: ['src'],
        alias: {
          _assets: './src/assets',
          _components: './src/components',
          _atoms: './src/components/atoms',
          _molecules: './src/components/molecules',
          _organisms: './src/components/organisms',
          _navigations: './src/navigations',
          _scenes: './src/scenes',
          _services: './src/services',
          _styles: './src/styles',
          _utils: './src/utils',
        },
      },
    },
  },
};

Read more about alias setup at Babel Plugin Module Resolver.


Enable editors to alias autocompletion

Create the jsconfig.json file and use the same alias that was defined in .babelrc. Check it out below:


{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "_assets": ["src/assets/*"],
      "_components": ["src/components/*"],
      "_atoms": ["src/components/atoms/*"],
      "_molecules": ["src/components/molecules/*"],
      "_organisms": ["src/components/organisms/*"],
      "_navigations": ["src/navigations/*"],
      "_scenes": ["src/scenes/*"],
      "_services": ["src/services/*"],
      "_styles": ["src/styles/*"],
      "_utils": ["src/utils/*"]
    }
  }
}

Once you have edited it, it’s time to test the alias. Let’s edit our src/index.js file by adding a test component as follows:

import React from 'react';
import {View,Text} from 'react-native';

const App = () => (
  <View>
    <Text>Hello World</Text>
  </View>
);

export default App;

Now in our index.js in the project root we will import the App component as follows:

import App from './src';

This way you will have your alias set up working on your project.


Atomic Components

We will not dive into the concepts of atomic, only the organization we have chosen to use in this project.

– Atoms – The smallest possible components, such as buttons, titles, inputs or event color pallets, animations, and fonts.

– Molecules – They are the composition of one or more components of atoms.

– Organisms – The combination of molecules that work together or even with atoms that compose more elaborate interfaces.

Remembering the directories we use to organize your components:

├── src
│   ├── components
│   │  ├── atoms
│   │  ├── molecules
│   │  ├── organisms

In each component directory, we have an index.js file that exports the specified category.

Let’s create a component called HelloWorld in src/atoms to understand the idea:

src/atoms/hello-world.js


import React from 'react';
import {Text} from 'react-native';

const HelloWorld = ({name}) => <Text>Hello World {name}!</Text>;

export default HelloWorld;

We export as follows:

src/atoms/index.js


export {default as HelloWorld} from './hello-world';

Now we can use this component in src/index.js:


import React from 'react';
import {HelloWorld} from '_atoms';

const App = () => <HelloWorld name="Helder Burato Berto" />;
export default App;

Note: The App.js in the project root can be removed, it will no longer be used.


Our Scenes

I have a habit of dividing every application screen as a scene and so each one has its directory.

We will define some scenes that will not be used this time, and then we will configure these navigations using the created screens.


.
├── src
│   ├── scenes
│   │  ├── login
│   │  │	 ├── index.js // LoginScreen
│   │  ├── home
│   │  │	 ├── index.js // HomeScreen
│   │  ├── about
│   │  │	 ├── index.js // AboutScreen

At the Login screen, we will add a navigation button to go to the Home screen. See below:


import React from 'react';
import {SafeAreaView, Text, TouchableHighlight} from 'react-native';

const LoginScreen = ({navigation}) => (
  <SafeAreaView>
    <Text>Screen: Login</Text>

    <TouchableHighlight onPress={() => navigation.navigate('Home')}>
      <Text>Go to home</Text>
    </TouchableHighlight>
  </SafeAreaView>
);

export default LoginScreen;

Note: The navigation object will be available on all screens that are surrounded by the navigator object.


Ways of Navigation

In this step, we will need to add some new dependencies to the project. See below:

$ yarn add react-navigation@^4.0.0 react-navigation-stack@^1.5.3
react-navigation-tabs@^2.4.0 react-native-gesture-handler@^1.4.1
react-native-reanimated@^1.2.0

You can read more about it https://reactnavigation.org/docs/en/getting-started.html.

In our application, we will have two types of navigation.

In the Login screen, we will have Stack navigation type and in the rest of the app we will have Tab navigation type.

Note: This is just an example of navigation, not a pattern. If you need to use other types of navigation, you can create them at the src/navigations.

In the src/navigations directory we will define the following structure:

├── src
│   ├── navigations
│   │  ├── index.js            // RootNavigator
│   │  ├── auth-navigator.js   // AuthNavigator
│   │  ├── app-navigator.js    // AppNavigator
  1. In the auth-navigator.js we will define the navigation type for the login screen:

    
    import {createStackNavigator} from 'react-navigation-stack';
    
    import LoginScreen from '_scenes/login';
    
    const AuthNavigatorConfig = {
    initialRouteName: 'Login',
    header: null,
    headerMode: 'none',
    };
    
    const RouteConfigs = {
    Login:LoginScreen,
    };
    
    const AuthNavigator = createStackNavigator(RouteConfigs, AuthNavigatorConfig);
    
    export default AuthNavigator;
    
  2. In the app-navigator.js we will define the type of navigation to internal screens app:

    
    import {createBottomTabNavigator} from 'react-navigation-tabs';
    
    import HomeScreen from '_scenes/home';
    import AboutScreen from '_scenes/about';
    
    const TabNavigatorConfig = {
    initialRouteName: 'Home',
    header: null,
    headerMode: 'none',
    };
    
    const RouteConfigs = {
    Home:{
        screen:HomeScreen,
    },
    About:{
        screen:AboutScreen,
    },
    };
    
    const AppNavigator = createBottomTabNavigator(RouteConfigs, TabNavigatorConfig);
    
    export default AppNavigator;
    
  3. In the index.js we will define our RootNavigator merging the auth and app navigators:

    
    import {createAppContainer, createSwitchNavigator} from 'react-navigation';
    
    import AuthNavigator from './auth-navigator';
    import AppNavigator from './app-navigator';
    
    const RootNavigator = createSwitchNavigator(
    {
        Auth: AuthNavigator,
        App: AppNavigator,
    },
    {
        initialRouteName: 'Auth',
    },
    );
    
    export default createAppContainer(RootNavigator);
    

    Now you can import the Navigator object into your src/index.js as follows:

    import React from 'react';
    
    import Navigator from '_navigations';
    
    const App = () => <Navigator />;
    
    export default App;
    

    This way you will have simple and functional navigation.


Reusable Services

Not everything can be considered a component in React Native, a well-known approach used to create separate modules and in some cases containing business rules are the use of services.

Directory for creating services:

*** src/services ***

They can be shared with multiple screens and components in your project.

Commonly used to create services that make contact with external APIs and use the axios library that we mentioned at the beginning of the post.


Shared Styles

Based in CSS preprocessor we use some default files in our style structure:

.
├── src
│   ├── styles
│   │  ├── index.js        // Export all
│   │  ├── colors.js       // Colors pallet
│   │  ├── mixins.js       // Mixins to use CSSinJS
│   │  ├── spacing.js      // Paddings, margins and scale
│   │  ├── typography.js   // Fonts types and sizes

index.js

import * as Colors from './colors';
import * as Spacing from './spacing';
import * as Typography from './typography';
import * as Mixins from './mixins';

export { Typography, Spacing, Colors, Mixins };

colors.js

export const PRIMARY = '#1779ba';
export const SECONDARY = '#767676';
export const WHITE = '#FFFFFF';
export const BLACK = '#000000';

// ACTIONS
export const SUCCESS = '#3adb76';
export const WARNING = '#ffae00';
export const ALERT = '#cc4b37';

// GRAYSCALE
export const GRAY_LIGHT = '#e6e6e6';
export const GRAY_MEDIUM = '#cacaca';
export const GRAY_DARK = '#8a8a8a';

mixins.js

import {Dimensions,PixelRatio} from 'react-native';

const WINDOW_WIDTH = Dimensions.get('window').width;
const guidelineBaseWidth = 375;

export const scaleSize = size => (WINDOW_WIDTH/guidelineBaseWidth) * size;

export const scaleFont = size => size * PixelRatio.getFontScale();

function dimensions(top, right = top, bottom = top, left = right, property){
  let styles = {};

  styles[`${property}Top`] = top;
  styles[`${property}Right`] = right;
  styles[`${property}Bottom`] = bottom;
  styles[`${property}Left`] = left;

  return styles;
}

export function margin(top, right, bottom, left){
  return dimensions(top, right, bottom, left, 'margin');
}

export function padding(top, right, bottom, left){
  return dimensions(top, right, bottom, left, 'padding');
}

export function boxShadow(color, offset = {height:2,width:2},
                           radius = 8, opacity = 0.2){
  return {
    shadowColor: color,
    shadowOffset: offset,
    shadowOpacity: opacity,
    shadowRadius: radius,
    elevation: radius,
  };
}

spacing.js


import {scaleSize} from './mixins';

export const SCALE_18 = scaleSize(18);
export const SCALE_16 = scaleSize(16);
export const SCALE_12 = scaleSize(12);
export const SCALE_8 = scaleSize(8);

typography.js


import { scaleFont } from './mixins';

// FONT FAMILY
export const FONT_FAMILY_REGULAR = 'OpenSans-Regular';
export const FONT_FAMILY_BOLD = 'OpenSans-Bold';

// FONT WEIGHT
export const FONT_WEIGHT_REGULAR = '400';
export const FONT_WEIGHT_BOLD = '700';

// FONT SIZE
export const FONT_SIZE_16 = scaleFont(16);
export const FONT_SIZE_14 = scaleFont(14);
export const FONT_SIZE_12 = scaleFont(12);

// LINE HEIGHT
export const LINE_HEIGHT_24 = scaleFont(24);
export const LINE_HEIGHT_20 = scaleFont(20);
export const LINE_HEIGHT_16 = scaleFont(16);

// FONT STYLE
export const FONT_REGULAR = {
  fontFamily: FONT_FAMILY_REGULAR,
  fontWeight: FONT_WEIGHT_REGULAR,
};

export const FONT_BOLD = {
  fontFamily: FONT_FAMILY_BOLD,
  fontWeight: FONT_WEIGHT_BOLD,
};

This is our basic style settings to structure our application.

This way you can import into any of your components the following Typography, Spacing, Colors, Mixins objects, which will have access to the functionality of each style object.

Extra: Custom Font To enable custom fonts you need to create the react-native.config.js in the project root and set the directory where your .ttf files are as follows:

module.exports = {
  assets:['./src/assets/fonts/'],
};

After that, you should run the react-native link to link your fonts to the iOS / Android native code.


Defining Utils

We have the src/utils directory where we add all our utility/helper methods that can be shared across our entire project.

Whenever you come across situations where you get caught repeating code is a good situation to create a util/helper.


Wrapping up

I have been working with this format on the last React Native projects I worked on and I can say that it helped me a lot regarding the organization and development of the project.

This is just one way we found to be productive and better organized among our team, I hope it helps you too.

Feel free to comment below if you have any questions, I’ll be happy to help you.

Enjoy programming!

The repository of this example is available at ReactNativeExample.

More From React Native Tutorial

Basics

1. Introduction To React Native

2. React Native Environment Setup using expo

3. React Native Environment Setup for windows

4. React Native Environment setup on Mac OS

5. React Native Environment setup on linux

6. React Native Project Structure

7. React Native State

8. React Native Props

9. React Native Styling

10. React Native Flexbox

11. React Native Text

12. React Native Textinput

13. React Native Commands

14. React Native ScrollView

Advances

1. React Native Dark Mode

2. React Native Fonts

3. React Native SQLite

4. React Native DatepickerAndroid

5. React native ScrollView scroll to position

6. How to align icon with text in react native

7. React Native Image

8. React Native Firebase Crashlytics

9. React Native Async Storage

10. React Native Share

Error & Issue Solution

1. Task :app:transformDexArchiveWithDexMergerForDebug FAILED In React Native

2. Expiring Daemon because JVM heap space is exhausted In React Native

3. Task :app:transformNativeLibsWithMergeJniLibsForDebug FAILED In React Native

4. Unable to determine the current character, it is not a string, number, array, or object in react native

5. App crashed immediately after install react native video or track player

6. how to delete SQLite database in android react native

7. React native material dropdown twice click issue

8. How to get the current route in react-navigation?

9. how to disable drawer on the drawer navigation screen?

10. Image not showing in ios 14 react native