React Native with Nx

Nx provides a holistic dev experience powered by an advanced CLI and editor plugins. It provides rich support for common tools like Detox, Storybook, Jest, and more.

In this guide we will show you how to develop React Native applications with Nx.

Creating Nx Workspace

The easiest way to create your workspace is via npx.

npx create-nx-workspace happynrwl \

--preset=react-native \

--appName=mobile

Don't know what you need?

You can also run the command without arguments to go through the interactive prompts.

npx create-nx-workspace happynrwl

Once the command completes, the workspace will look as follows:

1happynrwl/ 2├── apps/ 3│ ├── mobile/ 4│ │ ├── app.json 5│ │ ├── metro.config.js 6│ │ ├── android/ 7│ │ │ ├── app/ 8│ │ │ ├── gradle/ 9│ │ │ ├── build.gradle 10│ │ │ ├── gradle.properties 11│ │ │ ├── gradlew 12│ │ │ ├── settings.gradle 13│ │ ├── ios/ 14│ │ │ ├── Mobile/ 15│ │ │ ├── Mobile.xcodeproj/ 16│ │ │ ├── Mobile.xcworkspace/ 17│ │ │ ├── Podfile 18│ │ │ ├── Podfile.lock 19│ │ ├── src/ 20│ │ │ ├── main.tsx 21│ │ │ └── app/ 22│ │ │ ├── App.tsx 23│ │ │ └── App.spec.tsx 24│ │ ├── .babelrc 25│ │ ├── jest.config.ts 26│ │ ├── test-setup.ts 27│ │ ├── package.json 28│ │ ├── project.json 29│ │ ├── tsconfig.json 30│ │ ├── tsconfig.app.json 31│ │ └── tsconfig.spec.json 32│ └── mobile-e2e/ 33│ ├── .detoxrc.json 34│ ├── src/ 35│ │ └── app.spec.ts 36│ ├── .babelrc 37│ ├── jest.config.json 38│ ├── project.json 39│ ├── tsconfig.e2e.json 40│ └── tsconfig.json 41├── libs/ 42├── tools/ 43├── babel.config.json 44├── jest.config.ts 45├── jest.preset.js 46├── nx.json 47├── package-lock.json 48├── package.json 49└── tsconfig.base.json 50

To run the application in development mode:

npx nx start mobile

On Android simulator/device:

npx nx run-android mobile

iOS simulator/device:

npx nx run-ios mobile

Try out other commands as well.

  • nx lint mobile to lint the application
  • nx test mobile to run unit test on the application using Jest
  • nx sync-deps mobile to sync app dependencies to its package.json.

Release build

Android:

npx nx build-android mobile

iOS: (Mac only)

npx nx build-ios mobile

E2E

Android:

Inferred Tasks

Since Nx 18, Nx plugins can infer tasks for your projects based on the configuration of different tools. You can read more about it at the Inferred Tasks concept page.

npx nx test mobile-e2e -- --configuration="android.emu.debug"

iOS: (Mac only)

npx nx test mobile-e2e -- --configuration="ios.sim.debug"

When using React Native in Nx, you get the out-of-the-box support for TypeScript, Detox, and Jest.

Adding React Native to an Existing Workspace

For existing Nx workspaces, install the @nx/react-native package to add React Native capabilities to it.

nx add @nx/react-native

Generating an Application

To create additional React Native apps run:

npx nx g @nx/react-native:app mobile --directory=apps/mobile

Generating a Library

Nx allows you to create libraries with just one command. Some reasons you might want to create a library include:

  • Share code between applications
  • Publish a package to be used outside the monorepo
  • Better visualize the architecture using npx nx graph

To generate a new library run:

npx nx g @nx/react-native:lib shared-ui-layout --directory=libs/shared-ui-layout

And you will see the following:

1happynrwl/ 2├── apps/ 3│ └── mobile/ 4│ └── mobile-e2e/ 5├── libs/ 6│ └── shared-ui-layout/ 7│ ├── src/ 8│ │ └── index.ts 9│ ├── .babelrc 10│ ├── jest.config.js 11│ ├── project.json 12│ ├── README.md 13│ ├── test-setup.ts 14│ ├── tsconfig.json 15│ ├── tsconfig.lib.json 16│ └── tsconfig.spec.json 17├── tools/ 18├── babel.config.json 19├── jest.config.js 20├── jest.preset.js 21├── nx.json 22├── package-lock.json 23├── package.json 24└── tsconfig.base.json 25

Run:

  • npx nx test shared-ui-layout to test the library
  • npx nx lint shared-ui-layout to lint the library

To generate a new component inside shared-ui-layout run:

npx nx g @nx/react-native:component layout --directory=libs/shared-ui-layout/src/lib/layout --export

And you will see the following updated for shared-ui-layout:

1happynrwl/ 2└── libs/ 3 └── shared-ui-layout/ 4 └── src/ 5 ├── index.ts 6 └── lib/ 7 └── layout/ 8 ├── layout.tsx 9 └── layout.spec.tsx 10

Using Nx Library in your Application

You can import the shared-ui-layout library in your application as follows.

apps/mobile/src/app/App.tsx
1import React from 'react'; 2import { SafeAreaView } from 'react-native'; 3 4import { Layout } from '@happynrwl/shared-ui-layout'; 5 6const App = () => { 7 return ( 8 <SafeAreaView> 9 <Layout /> 10 </SafeAreaView> 11 ); 12}; 13 14export default App; 15

That's it! There is no need to build the library prior to using it. When you update your library, the React Native application will automatically pick up the changes.

Publishable libraries

For libraries intended to be built and published to a registry (e.g. npm) you can use the --publishable and --importPath options.

npx nx g @nx/react-native:lib shared-ui-layout --directory=libs/shared-ui-layout --publishable --importPath=@happynrwl/ui-components

npx nx g @nx/react-native:component layout --directory=libs/shared-ui-layout/src/lib/layout --export

Run npx nx build shared-ui-layout to build the library. It will generate the following:

1dist/libs/shared-ui-layout/ 2├── README.md 3├── index.d.ts 4├── lib/ 5│ └── layout/ 6│ └── layout.d.ts 7└── package.json 8

This dist folder is ready to be published to a registry.

Code Sharing

Without Nx, creating a new shared library can take from several hours to even weeks: a new repo needs to be provisioned, CI needs to be set up, etc... In an Nx Workspace, it only takes minutes.

You can share React Native components between multiple React Native applications, share business logic code between React Native mobile applications and plain React web applications. You can even share code between the backend and the frontend. All of these can be done without any unnecessary ceremony.