This project aims to be a strong foundation for react-native applications. It provides a clear and organized structure, core dependencies, and boilerplate to jumpstart development.
- Node.js > 12 and npm (Recommended: Use nvm)
- Watchman
- Xcode 12
- Cocoapods 1.10.1
- JDK > 11
- Android Studio and Android SDK
- axios for networking.
- prop-types to type-check our components exposed properties.
- react-native-config to manage envionments.
- react-navigation navigation library.
- react-native-localization for string localization.
- react-native-mmkv-storage as storage solution.
- redux for state management.
- redux-persist as persistance layer.
- redux-thunk to dispatch asynchronous actions.
- jest and react-native-testing-library for testing.
Click the "Use this template" button above the file list, then use the Owner drop-down menu, and select the account you want to own the repository. Creating a repository from a template has the following advantages:
- A repository created from a template starts with a single commit.
- Commits to a repository created from a template do appear in your contribution graph.
- Creating a repository from a template starts a new project quickly.
You can start by cloning this repository and using react-native-rename. In the current state of this project, it should give you no issues at all, just run the script, delete your node modules and reinstall them and you should be good to go.
Keep in mind that this library can cause trouble if you are renaming a project that uses Pods
on the iOS side.
After that you should proceed as with any javascript project:
- Go to your project's root folder and run
npm install
. - If you are using Xcode 12.5 or higher got to /ios and execute
pod install --
repo-update` - Run
npm run ios
ornpm run android
to start your application!
(Using yarn: yarn ios
or yarn android
)
Note: Please read the Setup environments section that is below in this file for more information about the execution scripts.
If you want to roll on your own and don't want to use this as a template, you can create your project and then copy the /src
folder (which has all the code of your application) and update your index.js
.
Keep in mind that if you do this, you'll have to install and link all dependencies (as well as adding all the necessary native code for each library that requires it).
This template follows a very simple project structure:
src
: This folder is the main container of all the code inside your application.actions
: This folder contains all actions that can be dispatched to redux.assets
: Asset folder to store all images, vectors, etc.components
: Folder to store any common component that you use through your app (such as a generic button)constants
: Folder to store any kind of constant that you have.controllers
: Folder to store all your network logic (you should have one controller per resource).localization
: Folder to store the languages files.navigation
: Folder to store the navigators.reducers
: This folder should have all your reducers, and expose the combined result using itsindex.js
screens
: Folder that contains all your application screens/features.Screen
: Each screen should be stored inside its folder and inside it a file for its code and a separate one for the styles and tests.Screen.js
Screen.styles.js
Screen.test.js
selectors
: Folder to store your selectors for each reducer.storage
: Folder that contains the application storage logic.store
: Folder to put all redux middlewares and the store.test-utils
: Folder to store tests-related utilities and components.theme
: Folder to store all the styling concerns related to the application theme.App.js
: Main component that starts your whole app.index.js
: Entry point of your application as per React-Native standards.
To customize the splash screen (logo and background color) use the CLI provided in the official docs.
The template already has scripts to execute the project calling a specific environment defined into the package.json file. Keep in mind that if you are going to create new envs
you have to define the script to build the project properly.
To define which env you want to use, just keep the structure yarn [platform]: [environment]
DEV: yarn ios
or yarn android
STG: yarn ios:staging
or yarn android:staging
PROD: yarn ios:prod
o yarn android:prod
Also, you can use npm following the same rule as before: npm run ios:staging
Modify the environment variables files in root folder (.env.development
, .env.production
and .env.staging
)
A map associating builds with env files is already defined in app/build.gradle
you can modify or add environments if needed.
For multiple enviroments to work you would need to change -keep class [YOUR_PACKAGE_NAME].BuildConfig { *; }
on proguard-rules.pro
file.
The basic idea in iOS is to have one scheme per environment file, so you can easily alternate between them.
To create a new scheme:
-
In the Xcode menu, go to Product > Scheme > Edit Scheme
-
Click Duplicate Scheme on the bottom
-
Give it a proper name on the top left. For instance: "qa"
-
Then edit the newly created scheme to make it use a different env file. From the same "manage scheme" window:
Expand the "Build" tab on the left menu
- Click "Pre-actions", and under the plus sign select "New Run Script Action"
- Where it says "Type a script or drag a script file", type:
echo ".env.qa" > /tmp/envfile
replacing.env.qa
with your file.
-
Also, you will need to select the executable for the new schema:
Expand the "Run" tab on the left menu
- Under the "Executable" dropdown select the ".app" you would like to use for that schema
These are the steps to generate .apk
, .aab
and .ipa
files
- Generate an upload key
- Setting up gradle variables
- Go to the android folder
- Execute
./gradlew assemble[Env][BuildType]
Note: You have three options to execute the project
assemble:
Generates an apk that you can share with others.
install:
When you want to test a release build on a connected device.
bundle:
When you are uploading the app to the Play Store.
For more info please go to https://reactnative.dev/docs/signed-apk-android
- Go to the Xcode
- Select the schema
- Select 'Any iOS device' as target
- Product -> Archive
For more info please go to https://reactnative.dev/docs/publishing-to-app-store
For coding styling, we decided to go with ESLint and React Native community's styleguide.
The idea of this section is to explain how the template composition is the best and easiest to use when you try to use well-formed, architectures, especially using redux flow.
The template follows a simple and convenient exporting pattern. The folder index exposes the resources, allowing to import all from the same path.
With that in mind, we are going to look at each folder to explain how to use it.
Components are the basic blocks of a react native application, but since we aim to minimize development complexity, all the components are at the same nesting level.
Another important thing is the use of propTypes to check the kind of data that your components need to work properly. If the component receives some data from others, the type of these props must be defined, and in case you need it the default value of the property too.
To keep an application scalable and organized, the global static resources that are used in the application have to be created in a specific file.
-
Assets: Here you can store all the images and icons that you need through the app. You have as an example the icon ic_home.png, to respond with the different device screen densities just create inside the same folder the image and all the scaled versions that you need. RN only handles x1, x2 and x3 in this case, you have.
- assets
- ic_home
- ic_home.png
- [email protected]
- [email protected]
- ic_home
- assets
-
Localization: This folder contains all the locale objects that you need to create a multilingual application. Create a file for each locale, inside define an object then maintain the nesting sorted by the screen that contains the text that you need and the text you want to show. As the last step, remember to create a reference inside the Localization.js file and add it to LocalizedStrings.
-
Theme: Here you can define all the styles that you use on different screens. To make easier the interaction of the application with device options for example you can create here assets as light and dark color palette
Once the components are defined, they are tied to the management of information through the application. For this, Redux is implemented with the store-reducer-action structure as usual, however, not only the data is handled through the actions but the success and error responses are also defined by the same form.
To keep the networking layer simple, the template uses a single Axios instance in the httpClient.js
. It uses interceptors to define common side effects for the responses.
When you need communication with a service you have to create a function to manage the operation and grouping according to the kind of transaction inside a controller file, please keep all of those inside the controllers' folder.
While the data transfer between the API and the app is working you must use the success and error actions that help you to catch the result of the operation. With this method, you can track the interaction through the redux store. This is useful because you can create behaviors based on those states in a quick and simple way
4 folders divide the redux work
- Store: Here you define the store shape and you can configure the persistReducer and middlewares.
- Actions: Remember to create the file and the corresponding test for each action classification. Here you define actions for success and error scenarios.
- Reducers: You have the error and success reducers by default. Create the other classifications and try to keep simple each file. Here you modify the store.
- Selectors: Create one file for each action classification. Here you define what part of the store you need to see in your interface.
In this folder, you have the main objects to apply the composition architecture. Just create a folder for each screen you have in your application, call all the components and static resources you need to render the scene and finally use the corresponding hooks to interact with redux and create behaviors depending on the store.
To keep the structure, extract the styles from the main file and place it in a {namefile.styles.js} do the same for the set of tests needed for each screen with the file {namefile.test.js}