React Native — Modern Mobile Application Architecture and Design Patterns
This article covers the development of a cross-platform mobile application
If you are new to React Native please refer to https://reactnative.dev for basic details required to develop a cross-platform mobile application.
Our app will include all the modern features and plugins most React Native projects utilize such as Redux, Axios, Date-Fns, React-Navigation, etc. and our components and fonts will be responsive to device screens our mobile app will be installed by using packages like Responsive-Screen and Responsive-Fontsize.
TABLE OF CONTENTS
Section 1 - React Native Folder Structure
Section 2 - React Native Application Architecture
Section 3 - React Native Design Patterns
Section 1 - React Native Folder Structure
Our folder structure is the most basic element that makes our app run efficiently and it is also how we manage the project across teams, we have to make sure it is implemented in a way whereby we consider the hierarchy of the project contents and also the sub-elements and their folders too, here "Separation of Concerns" plays a major roles in helping us quickly make decisions of where things should belong.
1. Root Directory Folder
The root directory comes prepopulated with folders such as the ios, android, node_modules, etc. In the image below you can see folders generated from packages we have also installed such as ESLint, Prettier, EditorConfig, StyleLint, etc. To create a brand new app where we get the default project structure we use the below command...
npx react-native@latest init AwesomeProject
2. Src Folders:
Within the root directory, we can see there's a src folder which is short for "source", this is where we keep most of the files and folders that have to do mainly with the development of our app customizations.
3. Assets Folder
Here we separate our media files such as Images, SVG's, Fonts, etc. that have nothing to do with codes we are going to write within our app. We also separate these folders into their respective folders, this is also known as "Separation of Concerns".
4. Components and Screen Folder
Just like the assets folder we also break down our code files into sub-folders so it is easily identifiable where what code is written, and all components go into a main sub-folder and each folder within this components folder is properly labeled, the same thing happens within our screens folder, which is where the code for each screen created in our app will be located, here we also imbibe "Separation of Concerns" which is very important.
...
Section 2 - React Native Application Architecture
“package.json” is the definition of react-native projects. This is how it looks after setup is completed for a react-native app as of “12-jun-2020”:
Let’s first restructure the application code to make it more robust and scalable. This will make it easy for on-boarding new resources by defining a common approach for the development process.
Please find below the structure of the app and all the changes explained below:
/index.js :
/App/… :
Let’s dive-in to understand the rationale behind this structure:
/assets :
/components:
/config :
/i18n :
/navigation :
/redux :
/screens :
/services :
/styles :
<View styles={[ globalStyles.wrapper, styles.textWrap ]}>
…
</View>
Recommended by LinkedIn
/utils :
/__tests__ :
Section 2 - Design Patterns
Selecting a design pattern is an opinionated decision and should not impact the performance of the application. We are implementing the “Container — View pattern” in this application. One of the other famous design patterns is the “Atomic pattern”.
Section-3 Architecture Dependencies
We’ll start adding features/capabilities to our application one-by-one now. List of all the dependencies for our ReactNative project and its integration for a better architecture:
Step-1: React-navigation v5.5.1
(ref: https://reactnavigation.org/)
It will avail routing and navigation capability to our React native app. It provides us with various navigator implementations like Stack-navigation, Tabs-navigation or Drawer/Side-tray navigation.
React-navigation library also provides features like maintaining history of user journey along with gestures and animations that you may expect from a native mobile app.
$ npm install -S @react-navigation/native @react-navigation/stack
$ npm install react-native-reanimated react-native-gesture-handler react-native-screens react-native-safe-area-context @react-native-community/masked-view
Step-2: Unit testing framework
Most important: all documentation and APIs for unit testing can be found here.
Jest is default supported in react-native applications for unit testing. This will enable us to perform unit testing and generating code coverage reports.
Jest gives below features out of the box:
✓ Minimal configuration
✓ Watches only changed files
✓ Fast
✓ Snapshot testing (explained later)
✓ Coverage out of box
In this app, we’ll use ‘react-native-testing-library’ as a testing framework. React-native-testing-library will enable us to traverse through the element tree and simulate user actions. It provides light utility functions on top of react-test-renderer letting you always be up to date with latest React features and write any component tests.
Use below commands:
$ npm install — save-dev react-native-testing-library
$ npm install — save-dev @testing-library/jest-native
The 4 important test scripts we have
“scripts”: {
“test”: “jest — coverage”,
“test:update”: “jest — coverage — updateSnapshot”,
“test:watch”: “jest — watch”,
“coverage”: “jest — coverage && open ./coverage/lcov-report/index.html”,
}
Step-3: Internationalisation (I18n)
react-native-i18n is a library to enable multilingual support to our app. It comes handy with various features like getting the default language based on user local, fallback for missing translations, etc.
Use following command to add internationalisation in the app:
$ npm i — save react-native-i18n
Step-4: Redux & Thunk
(ref: https://redux.js.org/)
Redux is used for the provision of a central store. All the data that needs to be shared across features, modules or react-component tree, can be added to the redux store.
Thunk is the middleware that works hand-in-hand with redux for handling the side-effects like fetching data from database, REST APIs, application processing delays, etc. It is promise based.
$ npm install — save redux react-redux
$ npm install — save redux-thunk
Step-5: UI Library — Material design library (react-native-paper)
React Native Paper is a cross-platform UI component library which follows the material design guidelines, with global theming support and an optional babel-plugin to reduce bundle-size.
It uses vector-icons library, a set of customisable icons for React native with support for NavBar/TabBar/ToolbarAndroid, image source and full styling.
Use the below command to add it in the project.
$ npm install — save react-native-paper
$ npm install — save react-native-vector-icons
Quick look of the “package.json”:
Thats all folks!. This app will be able to perform excellently well without any scalability and maintenance issues. 😜