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:
1$ 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:
1$ react-native init ReactNativeExample
After the execution, you can access the directory using the cd ReactNativeExample. You will get a structure similar to this:
1.2├── __tests__3│ ├── App-test.js4├── android5├── ios6├── node_modules7├── .buckconfig8├── .eslintrc.js9├── .flowconfig10├── .gitattributes11├── .gitignore12├── .prettierrc.js13├── .watchmanconfig14├── App.js15├── app.json16├── babel.config.js17├── index.js18├── metro.config.js19├── package.json20└── 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:
1├── src2│ ├── assets3│ │ ├── fonts4│ │ ├── images5│ ├── components6│ │ ├── atoms7│ │ ├── molecules8│ │ ├── organisms9│ ├── navigations10│ ├── scenes11│ ├── styles12│ ├── utils13│ ├── 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:
1$ yarn add -D [email protected]^5.1.02[email protected]^2.18.2 [email protected]^3.2.0
After installing the dependencies, let’s configure the .babelrc:
.babelrc
1{2 "plugins": [3 [4 "module-resolver",5 {6 "cwd": "babelrc",7 "root": ["./src"],8 "extensions": [".js", ".ios.js", ".android.js"],9 "alias": {10 "_assets": "./src/assets",11 "_components": "./src/components",12 "_atoms": "./src/components/atoms",13 "_molecules": "./src/components/molecules",14 "_organisms": "./src/components/organisms",15 "_navigations": "./src/navigations",16 "_scenes": "./src/scenes",17 "_services": "./src/services",18 "_styles": "./src/styles",19 "_utils": "./src/utils"20 }21 }22 ]23 ]24}
Edit the .eslintrc.js file to avoid lint errors when using the new alias:
1module.exports = {2 root: true,3 extends: '@react-native-community',4 plugins: ['import'],5 settings: {6 'import/resolver': {7 node: {8 paths: ['src'],9 alias: {10 _assets: './src/assets',11 _components: './src/components',12 _atoms: './src/components/atoms',13 _molecules: './src/components/molecules',14 _organisms: './src/components/organisms',15 _navigations: './src/navigations',16 _scenes: './src/scenes',17 _services: './src/services',18 _styles: './src/styles',19 _utils: './src/utils',20 },21 },22 },23 },24};
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:
1{2 "compilerOptions": {3 "baseUrl": ".",4 "paths": {5 "_assets": ["src/assets/*"],6 "_components": ["src/components/*"],7 "_atoms": ["src/components/atoms/*"],8 "_molecules": ["src/components/molecules/*"],9 "_organisms": ["src/components/organisms/*"],10 "_navigations": ["src/navigations/*"],11 "_scenes": ["src/scenes/*"],12 "_services": ["src/services/*"],13 "_styles": ["src/styles/*"],14 "_utils": ["src/utils/*"]15 }16 }17}
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:
1import React from 'react';2import {View,Text} from 'react-native';34const App = () => (5 <View>6 <Text>Hello World</Text>7 </View>8);910export default App;
Now in our index.js in the project root we will import the App component as follows:
1import 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:
1├── src2│ ├── components3│ │ ├── atoms4│ │ ├── molecules5│ │ ├── 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
1import React from 'react';2import {Text} from 'react-native';34const HelloWorld = ({name}) => <Text>Hello World {name}!</Text>;56export default HelloWorld;
We export as follows:
src/atoms/index.js
1export {default as HelloWorld} from './hello-world';
Now we can use this component in src/index.js:
1import React from 'react';2import {HelloWorld} from '_atoms';34const App = () => <HelloWorld name="Helder Burato Berto" />;
1export 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.
1.2├── src3│ ├── scenes4│ │ ├── login5│ │ │ ├── index.js // LoginScreen6│ │ ├── home7│ │ │ ├── index.js // HomeScreen8│ │ ├── about9│ │ │ ├── index.js // AboutScreen
At the Login screen, we will add a navigation button to go to the Home screen. See below:
1import React from 'react';2import {SafeAreaView, Text, TouchableHighlight} from 'react-native';34const LoginScreen = ({navigation}) => (5 <SafeAreaView>6 <Text>Screen: Login</Text>78 <TouchableHighlight onPress={() => navigation.navigate('Home')}>9 <Text>Go to home</Text>10 </TouchableHighlight>11 </SafeAreaView>12);1314export 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:
1$ yarn add [email protected]^4.0.0 [email protected]^1.5.32[email protected]^2.4.0 [email protected]^1.4.13[email protected]^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:
1├── src2│ ├── navigations3│ │ ├── index.js // RootNavigator4│ │ ├── auth-navigator.js // AuthNavigator5│ │ ├── app-navigator.js // AppNavigator
In the auth-navigator.js we will define the navigation type for the login screen:
1import {createStackNavigator} from 'react-navigation-stack';23import LoginScreen from '_scenes/login';45const AuthNavigatorConfig = {6initialRouteName: 'Login',7header: null,8headerMode: 'none',9};1011const RouteConfigs = {12Login:LoginScreen,13};1415const AuthNavigator = createStackNavigator(RouteConfigs, AuthNavigatorConfig);1617export default AuthNavigator;In the app-navigator.js we will define the type of navigation to internal screens app:
1import {createBottomTabNavigator} from 'react-navigation-tabs';23import HomeScreen from '_scenes/home';4import AboutScreen from '_scenes/about';56const TabNavigatorConfig = {7initialRouteName: 'Home',8header: null,9headerMode: 'none',10};1112const RouteConfigs = {13Home:{14 screen:HomeScreen,15},16About:{17 screen:AboutScreen,18},19};2021const AppNavigator = createBottomTabNavigator(RouteConfigs, TabNavigatorConfig);2223export default AppNavigator;In the index.js we will define our RootNavigator merging the auth and app navigators:
1import {createAppContainer, createSwitchNavigator} from 'react-navigation';23import AuthNavigator from './auth-navigator';4import AppNavigator from './app-navigator';56const RootNavigator = createSwitchNavigator(7{8 Auth: AuthNavigator,9 App: AppNavigator,10},11{12 initialRouteName: 'Auth',13},14);1516export default createAppContainer(RootNavigator);Now you can import the Navigator object into your src/index.js as follows:
1import React from 'react';23import Navigator from '_navigations';45const App = () => <Navigator />;67export 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:
1.2├── src3│ ├── styles4│ │ ├── index.js // Export all5│ │ ├── colors.js // Colors pallet6│ │ ├── mixins.js // Mixins to use CSSinJS7│ │ ├── spacing.js // Paddings, margins and scale8│ │ ├── typography.js // Fonts types and sizes
index.js
1import * as Colors from './colors';2import * as Spacing from './spacing';3import * as Typography from './typography';4import * as Mixins from './mixins';56export { Typography, Spacing, Colors, Mixins };
colors.js
1export const PRIMARY = '#1779ba';2export const SECONDARY = '#767676';3export const WHITE = '#FFFFFF';4export const BLACK = '#000000';56// ACTIONS7export const SUCCESS = '#3adb76';8export const WARNING = '#ffae00';9export const ALERT = '#cc4b37';1011// GRAYSCALE12export const GRAY_LIGHT = '#e6e6e6';13export const GRAY_MEDIUM = '#cacaca';14export const GRAY_DARK = '#8a8a8a';
mixins.js
1import {Dimensions,PixelRatio} from 'react-native';23const WINDOW_WIDTH = Dimensions.get('window').width;4const guidelineBaseWidth = 375;56export const scaleSize = size => (WINDOW_WIDTH/guidelineBaseWidth) * size;78export const scaleFont = size => size * PixelRatio.getFontScale();910function dimensions(top, right = top, bottom = top, left = right, property){11 let styles = {};1213 styles[`${property}Top`] = top;14 styles[`${property}Right`] = right;15 styles[`${property}Bottom`] = bottom;16 styles[`${property}Left`] = left;1718 return styles;19}2021export function margin(top, right, bottom, left){22 return dimensions(top, right, bottom, left, 'margin');23}2425export function padding(top, right, bottom, left){26 return dimensions(top, right, bottom, left, 'padding');27}2829export function boxShadow(color, offset = {height:2,width:2},30 radius = 8, opacity = 0.2){31 return {32 shadowColor: color,33 shadowOffset: offset,34 shadowOpacity: opacity,35 shadowRadius: radius,36 elevation: radius,37 };38}
spacing.js
1import {scaleSize} from './mixins';23export const SCALE_18 = scaleSize(18);4export const SCALE_16 = scaleSize(16);5export const SCALE_12 = scaleSize(12);6export const SCALE_8 = scaleSize(8);
typography.js
1import { scaleFont } from './mixins';23// FONT FAMILY4export const FONT_FAMILY_REGULAR = 'OpenSans-Regular';5export const FONT_FAMILY_BOLD = 'OpenSans-Bold';67// FONT WEIGHT8export const FONT_WEIGHT_REGULAR = '400';9export const FONT_WEIGHT_BOLD = '700';1011// FONT SIZE12export const FONT_SIZE_16 = scaleFont(16);13export const FONT_SIZE_14 = scaleFont(14);14export const FONT_SIZE_12 = scaleFont(12);1516// LINE HEIGHT17export const LINE_HEIGHT_24 = scaleFont(24);18export const LINE_HEIGHT_20 = scaleFont(20);19export const LINE_HEIGHT_16 = scaleFont(16);2021// FONT STYLE22export const FONT_REGULAR = {23 fontFamily: FONT_FAMILY_REGULAR,24 fontWeight: FONT_WEIGHT_REGULAR,25};2627export const FONT_BOLD = {28 fontFamily: FONT_FAMILY_BOLD,29 fontWeight: FONT_WEIGHT_BOLD,30};
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:
1module.exports = {2 assets:['./src/assets/fonts/'],3};
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
- Introduction To React Native
- React Native Environment Setup using expo
- React Native Environment Setup for windows
- React Native Environment setup on Mac OS
- React Native Environment setup on linux
- React Native Project Structure
- React Native State
- React Native Props
- React Native Styling
- React Native Flexbox
- React Native Text
- React Native Textinput
- React Native Commands
- React Native ScrollView
Advances
- React Native Dark Mode
- React Native Fonts
- React Native SQLite
- React Native DatepickerAndroid
- React native ScrollView scroll to position
- How to align icon with text in react native
- React Native Image
- React Native Firebase Crashlytics
- React Native Async Storage
- React Native Share
Error & Issue Solution
- Task :app:transformDexArchiveWithDexMergerForDebug FAILED In React Native
- Expiring Daemon because JVM heap space is exhausted In React Native
- Task :app:transformNativeLibsWithMergeJniLibsForDebug FAILED In React Native
- Unable to determine the current character, it is not a string, number, array, or object in react native
- App crashed immediately after install react native video or track player
- how to delete SQLite database in android react native
- React native material dropdown twice click issue
- How to get the current route in react-navigation?
- how to disable drawer on the drawer navigation screen?
- Image not showing in ios 14 react native