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:

โฏ

npx nx test-android mobile-e2e

iOS: (Mac only)

โฏ

npx nx test-ios mobile-e2e

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.

โฏ

npm install @nx/react-native --save-dev

โฏ

# Or with yarn

โฏ

yarn add @nx/react-native --dev

Nx 15 and lower use @nrwl/ instead of @nx/

Generating an Application

To create additional React Native apps run:

โฏ

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

Nx 15 and lower use @nrwl/ instead of @nx/

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

For more information on Nx libraries, see our documentation on Creating Libraries and Library Types.

To generate a new library run:

โฏ

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

Nx 15 and lower use @nrwl/ instead of @nx/

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 --project=shared-ui-layout --export

Nx 15 and lower use @nrwl/ instead of @nx/

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 --project=shared-ui-layout --export

Nx 15 and lower use @nrwl/ instead of @nx/

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.