Home

Awesome

react-native-template

Build Status npm downloads npm version

<p align="center" > <img height="480px" src="docs/assets/preview.png" alt="Template landing screen preview" /> <img height="450px" src="docs/assets/reactotron.png" alt="Reactotron preview" /> </p> <br/>

A minimal template with architecture and boilerplate to let you focus on writing features right away.

Preconfigured with

Contents

Documentation

Getting Started

Create a new project using the template.

RN 0.63.2

$ npx react-native init MyApp --template @osamaq/react-native-template

Optional Steps

Connect To Sentry

This template comes with Sentry, but its disabled until you connect your account.

To connect your account:

$ cd MyApp/

# For MacOS
$ npx sentry-wizard -i reactNative -p ios android

# Other Platforms
$ npx sentry-wizard -i reactNative -p android

Insert your sentry DSN in each .env file (dev, staging and production) and you're all done.

Install Reactotron Flipper Plugin

This allows you to use Reactotron within Flipper.

Flipper -> Manage Plugins -> Install Plugins -> flipper-plugin-reactotron

Libraries

Let's briefly go over the benefit of each library included in this template.

TypeScript

For type safety ¯\(ツ)

But in all seriousness, if you are considering this template I assume you are a TypeScript fan. If you happen to be a JavaScript user, this template might be overwhelming. If you would like to start learning TypeScript, I suggest bootstrapping with this instead react-native-community/react-native-template-typescript so you can learn at your own pace.

SWR

This library simplifies data fetching and cache management. It allows you to easily show cached data, while the new data is being loaded from the API.

apisauce

Its a wrapper around axios with extended functionality. I'm happy with its API and I like the problem matcher.

Redux/Redux Toolkit

I'm happy using Redux Toolkit. It's a lot more concise now and I enjoy the redux ecosystem of plugins.

SWR reduces our dependency on Redux for global state. And sometimes React Navigation can be used to send data to the next screen. I try to leverage these two before reaching out to global state.

If you prefer something else, remove redux and go with that. Do not waste time trying a new state management solution.

Redux Observable

This is used alongside Redux for complex background work. Most people will rarely need to use something like this. In fact, if you aren't sure, just remove it (also uninstall its dependency, rxjs).

React Navigation

It is the most popular navigation library. For most apps, this is the best choice.

Reactotron/Flipper

Using this template there will be two main deubgging tools in your toolbelt. Reactotron and Flipper.

I mainly use Reactotron for reading API calls, asyncstorage operations, redux actions etc. It organizes everything in a neat way. It also has a killer image overlay feature, which allows you to get the UI design pixel prefect.

Flipper could be used for lower level debugging, such as viewing your database or React component tree. There is also a Flipper plugin for using Reactotron within it, so you only need to start one application.

Flipper -> Manage Plugins -> Install Plugins -> flipper-plugin-reactotron

Sentry

Benefitial in debugging issues that occur only in release builds. You can view error stack traces for unhandled exceptions. You can also choose to log specific errors in some catch blocks to study how often they occur in production.

In this template, there is a custom Redux middleware that adds Redux actions as breadcrumbs to Sentry reports for even easier debugging.

This is similar to redux-sentry-middleware but I've yet to test that one.

react-native-bootsplash

Works great for controlling your splash screen.

react-native-svg

Prefer using SVG over images all the time (remember to optimize your SVGs).

react-native-config

If you have different development, staging and production variables, this library is very helpful. It allows you to declare environment variables that can be accessed by all 3 sides (android, ios, JavaScript).

Android: by default, running react-native run-android will use the development .env file. To load .env.staging we must use:

ENVFILE=.env.staging react-native run-android

Note: the above works on MacOS. For windows its a bit different. See Different Environments.

iOS: two additional schemes are created in the Xcode project for staging and production. The corresponding .env file is set via the scheme's pre-action:

<div align="center"> <img src="docs/assets/scheme.png" alt="Scheme's pre-action setting the .env file" width="100%"> </div>

NPM scripts for running the app with the desired configuration are included for convenience.

Reanimated/Redash

Necessary when creating complex gesture based animations that are highly performant. Redash contains boilerplate helpers for Reanimated.

AsyncStorage

For caching simple data such as user perferences.

FastImage

Drop in replacement for the <Image/> component. I've found this to give a performance boost on android when rendering many images.

Detox

For end-to-end testing.

Mirage JS

Mirage is an in-memory server for intercepting API calls and returning whatever data you want. Very useful for developing before the backend is deployed, and for confirming how the app reacts to different API call outcomes.

Fastlane

Fastlane community has an endless amount of mobile development automation plugins. I currently use it mainly for automatic versioning, and often for deploying to Microsoft's App Center in one command.

This template also has a fastlane command for adding version badges to app icons. Useful outside of production as it makes it easier for QA to tell the app version.

<div align="center"> <img src="docs/assets/ic_launcher.png" alt="App icon with version badge" width="20%"> </div>

If you appreciate those libraries and find them useful, please consider supporting them.

Directory Structure

root
├── __tests__
├── android
├── e2e
├── fastlane
├── ios
├── scripts
└── src
    └── common
    |   ├── assets
    |   ├── components
    |   ├── exceptions
    |   ├── helpers
    |   ├── hooks
    |   ├── theme
    |   └── types
    └── features
    |   ├── error-boundary
    |   ├── home
    |   ├── landing
    |   └── navigation
    └── redux
    |   ├── middleware
    |   └── slices
    └── services
        ├── cache
        ├── navigation
        └── network
            ├── github
            └── mock

Quick Overview

Quickly get an idea about each folder's role.

DirectoryShort Description
rootRoot directory. Contains many configuration files and all other folders.
__tests__(Default; as per official template)
androidAndroid project. Includes modifications to integrate libraries.
e2eDetox end-to-end tests and configurations.
fastlaneUseful fastlane automation scripts.
iosiOS project. Includes modifications to integrate libraries.
scriptsHandy node scripts for code generation.
srcMost of the app's code is here.
commonShared code between different features.
assetsShared images, fonts etc.
componentsShared React components.
exceptionsShared custom exceptions.
helpersShared utlities.
hooksShared hooks.
themeShared styles; app's theme.
typesShared general types.
featuresFeature directories.
error-boundaryRoot error boundary.
homeHome screen. Has simple data fetching and global state examples.
landingTemplate's initial screen.
navigationContains a simple stack navigator.
reduxRedux integration.
middlewareRedux custom middleware. For now, a simple Sentry breadcrumb logger.
slicesRedux state slices.
servicesApp's services.
cacheCache service; AsyncStorage wrapper.
navigationNavigation service (navigate from outside React components).
networkNetworking related services.
githubExample GitHub API client (REST).
mockMirageJS in-memory server for mocking backend APIs.

If you would like to learn more without going through the codebase, read the file walkthrough here.

Credits

This template is modified from react-native-typescript-template. Thank you ❤️