|
| 1 | +# react-native-template |
| 2 | + |
| 3 | +[](https://travis-ci.com/osamaq/react-native-template) |
| 4 | +[](https://www.npmjs.com/package/@osamaq/react-native-template) |
| 5 | +[](https://www.npmjs.com/package/@osamaq/react-native-template) |
| 6 | + |
| 7 | +<p align="center" > |
| 8 | + <img |
| 9 | + height="480px" |
| 10 | + src="docs/assets/preview.png" |
| 11 | + alt="Template landing screen preview" |
| 12 | + /> |
| 13 | +</p> |
| 14 | + |
| 15 | +<br/> |
| 16 | + |
| 17 | +An opinionated template to bootstrap your next React Native app with all the time-wasting packages you need to have. |
| 18 | + |
| 19 | +## :computer: Contributions are very welcome 🤝 |
| 20 | + |
| 21 | +### Let's build the best React Native template together 🚀 |
| 22 | + |
| 23 | +Preconfigured with |
| 24 | + |
| 25 | +- Synced with the recommended [TypeScript template](https://reactnative.dev/docs/typescript#getting-started-with-typescript) |
| 26 | +- [Storybook](https://github.com/storybookjs/storybook/) The UI component explorer. Develop, document, & test React Native components |
| 27 | +- [React Query](https://github.com/tannerlinsley/react-query) for api calls |
| 28 | +- [Axios](https://github.com/tannerlinsley/react-query) for fetch api calls |
| 29 | +- [Redux Toolkit](https://redux-toolkit.js.org/) for global state. |
| 30 | +- [React Navigation](https://reactnavigation.org/) (**v5**) for navigation and deeplinking. |
| 31 | +- [styled-components](https://github.com/styled-components/styled-components) |
| 32 | +- [styled-system](https://github.com/styled-system/styled-system) |
| 33 | +- [react-native-bootsplash](https://github.com/zoontek/react-native-bootsplash) because splash screens are cool. |
| 34 | +- [react-native-svg](https://github.com/react-native-community/react-native-svg) because svg. |
| 35 | +- [react-native-config](https://github.com/luggit/react-native-config) to manage separate environments (dev, staging, production). |
| 36 | +- [Reanimated](https://software-mansion.github.io/react-native-reanimated/) for animations. |
| 37 | +- [react-native-keychain](https://github.com/oblador/react-native-keychain) for Keychain/Keystore access. |
| 38 | +- [react-native-permissions](https://github.com/zoontek/react-native-permissions) unified permissions api for iOS and Android. |
| 39 | +- [NetInfo](https://github.com/react-native-netinfo/react-native-netinfo) Network Info API for Android, iOS, macOS & Windows. |
| 40 | +- [react-native-appstate-hook](https://github.com/amrlabib/react-native-appstate-hook) handle app state in your app. |
| 41 | +- [react-native-file-logger](https://github.com/BeTomorrow/react-native-file-logger) to log important stuff. |
| 42 | +- [react-i18next](https://github.com/i18next/react-i18next) internationalization. |
| 43 | +- [AsyncStorage](https://github.com/react-native-community/async-storage) you're gonna install it anyway. |
| 44 | +- [FastImage](https://github.com/DylanVann/react-native-fast-image) its more performant |
| 45 | +- [Mirage JS](https://miragejs.com/) the easiest way to mock APIs. |
| 46 | +- [Fastlane](http://fastlane.tools/) for automation. |
| 47 | +- [Code Push](https://github.com/microsoft/react-native-code-push) syncronize JavaScript and Images with over-the-air updates. |
| 48 | +- handy npm scripts. |
| 49 | +- [react-native-testing-library](https://callstack.github.io/react-native-testing-library/) for unit and component tests. |
| 50 | +- [Sentry](https://github.com/getsentry/sentry-react-native) for debugging in production. |
| 51 | +- [Detox](https://github.com/wix/Detox) for e2e. |
| 52 | + |
| 53 | +## Contents |
| 54 | + |
| 55 | +- [Documentation](#documentation) |
| 56 | +- [Getting Started](#getting-started) |
| 57 | +- [Optional Steps](#optional-steps) |
| 58 | +- [Credits](#credits) |
| 59 | + |
| 60 | +## Documentation |
| 61 | + |
| 62 | +- [Libraries](#libraries) |
| 63 | +- [Directory Structure](#directory-structure) |
| 64 | +- [Quick Overview](#quick-overview) |
| 65 | +- [File Walkthrough](./docs/file-walkthrough.md) |
| 66 | + |
| 67 | +## Getting Started |
| 68 | + |
| 69 | +Create a new project using the template. |
| 70 | + |
| 71 | +- **Note:** the command will fail if you have the global legacy react-native-cli installed. Make sure you uninstall it first. More info at [react-native-community/cli](https://github.com/react-native-community/cli#about). |
| 72 | + |
| 73 | +### RN 0.64.1 |
| 74 | + |
| 75 | +```bash |
| 76 | +$ npx react-native init MyApp --template @hmarques98/react-native-template-typescript |
| 77 | +``` |
| 78 | + |
| 79 | +## Road Map |
| 80 | + |
| 81 | +- Finish Detox integration |
| 82 | +- Add Docusaurs docs |
| 83 | +- [Codepush Code Signing](https://github.com/microsoft/code-push/tree/v3.0.1/cli#code-signing) |
| 84 | + |
| 85 | +## Optional Steps |
| 86 | + |
| 87 | +#### Connect To Sentry |
| 88 | + |
| 89 | +This template comes with [Sentry](https://github.com/getsentry/sentry-react-native), but its disabled until you connect your account. |
| 90 | + |
| 91 | +To connect your account: |
| 92 | + |
| 93 | +```bash |
| 94 | +$ cd MyApp/ |
| 95 | + |
| 96 | +# For MacOS |
| 97 | +$ npx sentry-wizard -i reactNative -p ios android |
| 98 | + |
| 99 | +# Other Platforms |
| 100 | +$ npx sentry-wizard -i reactNative -p android |
| 101 | +``` |
| 102 | + |
| 103 | +Insert your sentry DSN in each [.env](https://github.com/osamaq/react-native-template/blob/ed37c213eacf0681c4f50f959bad170d46be0ed7/template/.env.prod#L5) file (dev, staging and production) and you're all done. |
| 104 | + |
| 105 | +### Setup deep linking scheme |
| 106 | + |
| 107 | +Implementation was based on react-navigation[instructions](https://reactnavigation.org/docs/deep-linking/#set-up-with-bare-react-native-projects). |
| 108 | + |
| 109 | +1. Update the prefix with wanted URI in `App.tsx` |
| 110 | + |
| 111 | +1. iOS |
| 112 | + |
| 113 | +Update `CFBundleURLName` and `CFBundleURLSchemes` to your desired URI |
| 114 | + |
| 115 | +1. Android |
| 116 | + |
| 117 | +Update `android:scheme` value inside `<data>` tag from the `<intent-filter>` |
| 118 | + |
| 119 | +#### Enable Push Notification capability from Xcode |
| 120 | + |
| 121 | +You must be a member of the Apple Developer Program in order to enable Push Notifications |
| 122 | + |
| 123 | + |
| 124 | + |
| 125 | +#### Customize notification appearance on Android |
| 126 | + |
| 127 | +Starting Android 10 (API 29), notification icons should be glyphs. You need to follow this step to [customize the notification appearance](https://wix.github.io/react-native-notifications/docs/installation-android/#step-6-customize-the-appearance). |
| 128 | + |
| 129 | +### Setup Code Push |
| 130 | + |
| 131 | +1. You will need to have [AppCenter CLI](https://github.com/microsoft/appcenter-cli) installed |
| 132 | + |
| 133 | +1. Creating an AppCenter account and/or organization and create an app for each platform and environment |
| 134 | + |
| 135 | +1. Copy and Replace the CodePushDeploymentKey in android/app/build.gradle under `buildTypes` |
| 136 | + |
| 137 | +1. Copy and Replace the CodePushDeploymentKey in android/app/build.gradle and XCode -> YourProject -> Build Settings -> `CODE_PUSH_KEY` |
| 138 | + |
| 139 | +You can retrieve this values by running `appcenter codepush deployment list -a <ownerName>/<appName> -k` |
| 140 | + |
| 141 | +#### Add project email for the FileLogger |
| 142 | + |
| 143 | +Update project email used by `sendLoggedFiles` method in `Home.tsx` |
| 144 | + |
| 145 | +## Libraries |
| 146 | + |
| 147 | +Let's briefly go over the benefit of each library included in this template. |
| 148 | + |
| 149 | +### TypeScript |
| 150 | + |
| 151 | +For type safety ¯\\_(ツ)_/¯ |
| 152 | + |
| 153 | +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](https://github.com/react-native-community/react-native-template-typescript) so you can learn at your own pace. |
| 154 | + |
| 155 | +### styled-components |
| 156 | + |
| 157 | +Style your apps without stress with dynamic styling and painless maintenance. `styled-components` utilises tagged template literals to style your component. Checkout this [Getting started tutorial](https://styled-components.com/docs/basics#getting-started). |
| 158 | + |
| 159 | +### styled-system |
| 160 | + |
| 161 | +Styled System is a collection of utility functions that add style props to your React components and allows you to control styles based on a global theme object with typographic scales, colors, and layout properties. Checkout this [Getting started tutorial](https://styled-system.com/#getting-started). |
| 162 | + |
| 163 | +### React Query |
| 164 | + |
| 165 | +Hooks for fetching, caching and updating asynchronous data in React. |
| 166 | + |
| 167 | +- Alternative: SWR/Apollo. |
| 168 | + |
| 169 | +### AXIOS |
| 170 | + |
| 171 | +## #TODO BENEFITS |
| 172 | + |
| 173 | +### Redux/Redux Toolkit |
| 174 | + |
| 175 | +I'm happy using Redux Toolkit. It's a lot more concise now and I enjoy the redux ecosystem of plugins. |
| 176 | + |
| 177 | +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. |
| 178 | + |
| 179 | +If you prefer something else, remove redux and go with that. Do not waste time trying a new state management solution. |
| 180 | + |
| 181 | +### React Navigation |
| 182 | + |
| 183 | +It is the most popular navigation library. For most apps, this is the best choice. |
| 184 | + |
| 185 | +### react-native-keychain |
| 186 | + |
| 187 | +Save and use credentials from phone's Keychain/Keystore |
| 188 | + |
| 189 | +### react-native-permissions |
| 190 | + |
| 191 | +A unified permissions API for React Native on iOS and Android. For iOS you have to the `Podfile` with permissions pod and update `Info.plist` with wanted permissions descriptions. For Android add wanted permissions in `AndroidManifest.xml` |
| 192 | + |
| 193 | +### react-i18next |
| 194 | + |
| 195 | +Localization using hooks. [React Native example](https://github.com/i18next/react-i18next/tree/master/example/react-native) |
| 196 | + |
| 197 | +### Codepush |
| 198 | + |
| 199 | +A React Native app is composed of JavaScript files and any accompanying images, which are bundled together by the packager and distributed as part of a platform-specific binary (i.e. an .ipa or .apk file). Once the app is released, updating either the JavaScript code (e.g. making bug fixes, adding new features) or image assets, requires you to recompile and redistribute the entire binary, which of course, includes any review time associated with the store(s) you are publishing to. |
| 200 | + |
| 201 | +The CodePush plugin helps get product improvements in front of your end users instantly, by keeping your JavaScript and images synchronized with updates you release to the CodePush server. This way, your app gets the benefits of an offline mobile experience, as well as the "web-like" agility of side-loading updates as soon as they are available. It's a win-win! |
| 202 | + |
| 203 | +### Sentry |
| 204 | + |
| 205 | +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. |
| 206 | + |
| 207 | +In this template, there is a custom Redux [middleware](https://github.com/osamaq/react-native-template/blob/master/template/src/redux/middleware/sentryMiddleware.ts) that adds Redux actions as breadcrumbs to Sentry reports for even easier debugging. |
| 208 | + |
| 209 | +This is similar to [redux-sentry-middleware](https://github.com/vidit-sh/redux-sentry-middleware) but I've yet to test that one. |
| 210 | + |
| 211 | +### react-native-bootsplash |
| 212 | + |
| 213 | +Works great for controlling your splash screen. |
| 214 | + |
| 215 | +### react-native-svg |
| 216 | + |
| 217 | +Prefer using SVG over images all the time (remember to optimize your SVGs). |
| 218 | + |
| 219 | +### react-native-config |
| 220 | + |
| 221 | +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). |
| 222 | + |
| 223 | +Android: by default, running `react-native run-android` will use the development .env file. To load .env.staging we must use: |
| 224 | + |
| 225 | +`ENVFILE=.env.staging react-native run-android` |
| 226 | + |
| 227 | +> Note: the above works on MacOS. For windows its a bit different. See [Different Environments](https://github.com/luggit/react-native-config#different-environments). |
| 228 | +
|
| 229 | +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: |
| 230 | + |
| 231 | +<div align="center"> |
| 232 | + <img src="docs/assets/scheme.png" alt="Scheme's pre-action setting the .env file" width="100%"> |
| 233 | +</div> |
| 234 | + |
| 235 | +NPM scripts for running the app with the desired configuration are [included](https://github.com/osamaq/react-native-template/blob/acc4f4ab117bee099a531ad44be1130f9d24df69/template/package.json#L11) for convenience. |
| 236 | + |
| 237 | +### Reanimated/Redash |
| 238 | + |
| 239 | +Necessary when creating complex gesture based animations that are highly performant. Redash contains boilerplate helpers for Reanimated. |
| 240 | + |
| 241 | +### react-native-appstate-hook |
| 242 | + |
| 243 | +Custom react hook, built to handle iOS or Android appState in your react component. |
| 244 | + |
| 245 | +### react-native-file-logger |
| 246 | + |
| 247 | +A simple file-logger for React Native with configurable rolling policy, based on CocoaLumberjack on iOS and Logback on Android. |
| 248 | + |
| 249 | +### AsyncStorage |
| 250 | + |
| 251 | +For caching simple data such as user perferences. |
| 252 | + |
| 253 | +### FastImage |
| 254 | + |
| 255 | +Drop in replacement for the `<Image/>` component. I've found this to give a performance boost on android when rendering many images. |
| 256 | + |
| 257 | +### NetInfo |
| 258 | + |
| 259 | +Network Info API for Android, iOS, macOS & Windows. It allows you to get information on: |
| 260 | + |
| 261 | +- Connection type |
| 262 | +- Connection quality |
| 263 | + |
| 264 | +### react-native-testing-library |
| 265 | + |
| 266 | +For unit and component testing. |
| 267 | + |
| 268 | +### Detox |
| 269 | + |
| 270 | +For end-to-end testing. |
| 271 | + |
| 272 | +### Mirage JS |
| 273 | + |
| 274 | +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. |
| 275 | + |
| 276 | +### Fastlane |
| 277 | + |
| 278 | +Fastlane community has an endless amount of mobile development automation plugins. I currently use it mainly for [automatic versioning](https://osamaq.com/automatic-versioning-for-react-native-apps/), and often for deploying to Microsoft's App Center in [one command](https://github.com/osamaq/reactnative-fastlane-appcenter). |
| 279 | + |
| 280 | +This template also has a [fastlane command](https://github.com/osamaq/react-native-template/blob/acc4f4ab117bee099a531ad44be1130f9d24df69/template/fastlane/Fastfile#L203) for adding version badges to app icons. Useful outside of production as it makes it easier for QA to tell the app version. |
| 281 | + |
| 282 | +<div align="center"> |
| 283 | + <img src="docs/assets/ic_launcher.png" alt="App icon with version badge" width="20%"> |
| 284 | +</div> |
| 285 | + |
| 286 | +--- |
| 287 | + |
| 288 | +> _If you appreciate those libraries and find them useful, please consider supporting them._ |
| 289 | +
|
| 290 | +## Directory Structure |
| 291 | + |
| 292 | +``` |
| 293 | +root |
| 294 | +├── __tests__ |
| 295 | +├── android |
| 296 | +├── ios |
| 297 | +├── storybook |
| 298 | +| └── addons.js |
| 299 | +| └── index.js |
| 300 | +| └── rn_addons.js |
| 301 | +└── src |
| 302 | + └── components |
| 303 | + | └── Counter.tsx |
| 304 | + | └── CustomScreen.tsx |
| 305 | + | └── CustomText.tsx |
| 306 | + | └── LanguageButton.stories.tsx |
| 307 | + | └── LanguageButton.tsx |
| 308 | + | └── StatusBar.tsx |
| 309 | + | └── stories.ts |
| 310 | + └── hooks |
| 311 | + | └── useCustomBackBehaviour.tsx |
| 312 | + | └── useNetworkError.tsx |
| 313 | + | └── useNotification.tsx |
| 314 | + | └── usePrevious.tsx |
| 315 | + | └── useStartupTime.tsx |
| 316 | + └── localization |
| 317 | + | └── resources |
| 318 | + | | └── en.json |
| 319 | + | | └── index.ts |
| 320 | + | | └── ro.json |
| 321 | + | └── index.tsx |
| 322 | + └── modules |
| 323 | + | └── auth |
| 324 | + | | └── hooks |
| 325 | + | | | └── useKeychainBiometrics.tsx |
| 326 | + | | | └── useKeychainCredentials.tsx |
| 327 | + | | | └── useSupportedBiometry.tsx |
| 328 | + | | └── screens |
| 329 | + | | | └── index.tsx |
| 330 | + | | | └── Onboarding.tsx |
| 331 | + | | | └── ResetPassword.tsx |
| 332 | + | | └── store |
| 333 | + | | | └── index.ts |
| 334 | + | └── <your_app_modules> |
| 335 | + └── navigation |
| 336 | + | ├── RootNavigation.tsx |
| 337 | + | └── Router.tsx |
| 338 | + └── screens |
| 339 | + | ├── CustomWebView.tsx |
| 340 | + | ├── Home.tsx |
| 341 | + | ├── index.ts |
| 342 | + | └── NetworkError.tsx |
| 343 | + └── utils |
| 344 | + ├── colors.ts |
| 345 | + └── console.ts |
| 346 | +
|
| 347 | +``` |
| 348 | + |
| 349 | +## Credits |
| 350 | + |
| 351 | +This template is modified from [react-native-typescript-template](https://github.com/react-native-community/react-native-template-typescript) and inspired from Osama Qarem's [React Native template](https://github.com/osamaqarem/react-native-template) and also inspired from Gabriel Moncea [React Native template](https://github.com/gabrielmoncea/react-native-template) |
| 352 | + |
| 353 | +Thank you ❤️ |
0 commit comments