React Native with Nx

React Logo

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

Note: 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:

happynrwl/
ā”œā”€ā”€ apps/
ā”‚   ā”œā”€ā”€ mobile/
ā”‚   ā”‚   ā”œā”€ā”€ app.json
ā”‚   ā”‚   ā”œā”€ā”€ metro.config.js
ā”‚   ā”‚   ā”œā”€ā”€ android/
ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ app/
ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ gradle/
ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ build.gradle
ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ gradle.properties
ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ gradlew
ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ settings.gradle
ā”‚   ā”‚   ā”œā”€ā”€ ios/
ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ Mobile/
ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ Mobile.xcodeproj/
ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ Mobile.xcworkspace/
ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ Podfile
ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ Podfile.lock
ā”‚   ā”‚   ā”œā”€ā”€ src/
ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ main.tsx
ā”‚   ā”‚   ā”‚   ā””ā”€ā”€ app/
ā”‚   ā”‚   ā”‚       ā”œā”€ā”€ App.tsx
ā”‚   ā”‚   ā”‚       ā””ā”€ā”€ App.spec.tsx
ā”‚   ā”‚   ā”œā”€ā”€ .babelrc
ā”‚   ā”‚   ā”œā”€ā”€ jest.config.js
ā”‚   ā”‚   ā”œā”€ā”€ test-setup.ts
ā”‚   ā”‚   ā”œā”€ā”€ package.json
ā”‚   ā”‚   ā”œā”€ā”€ project.json
ā”‚   ā”‚   ā”œā”€ā”€ tsconfig.json
ā”‚   ā”‚   ā”œā”€ā”€ tsconfig.app.json
ā”‚   ā”‚   ā””ā”€ā”€ tsconfig.spec.json
ā”‚   ā””ā”€ā”€ mobile-e2e/
ā”‚       ā”œā”€ā”€ .detoxrc.json
ā”‚       ā”œā”€ā”€ src/
ā”‚       ā”‚   ā””ā”€ā”€ app.spec.ts
ā”‚       ā”œā”€ā”€ .babelrc
ā”‚       ā”œā”€ā”€ jest.config.json
ā”‚       ā”œā”€ā”€ project.json
ā”‚       ā”œā”€ā”€ tsconfig.e2e.json
ā”‚       ā””ā”€ā”€ tsconfig.json
ā”œā”€ā”€ libs/
ā”œā”€ā”€ tools/
ā”œā”€ā”€ babel.config.json
ā”œā”€ā”€ jest.config.js
ā”œā”€ā”€ jest.preset.js
ā”œā”€ā”€ nx.json
ā”œā”€ā”€ package-lock.json
ā”œā”€ā”€ package.json
ā”œā”€ā”€ tsconfig.base.json
ā””ā”€ā”€ workspace.json

Run npx nx run-android mobile to run the applicatoin in development mode on Android simulator/device. Run npx nx run-ios mobile to run the applicatoin in developement mode on iOS simulator/device.

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 serve mobile to serve the application Javascript bundler that communicates with connected devices. This will start the bundler at http://localhost:8081.
  • nx sync-deps mobile to sync app dependencies to its package.json.

Release build

Android:

1npx nx build-android mobile

iOS: (Mac only)

No CLI support yet. Run in the Xcode project. See: https://reactnative.dev/docs/running-on-device

E2E

Android:

1npx nx test-android mobile-e2e

iOS: (Mac only)

1npx nx test-ios mobile-e2e

When using React Native in Nx, you get the out-of-the-box support for TypeScript, Detox, and Jest. No need to configure anything: watch mode, source maps, and typings just work.

Adding React Native to an Existing Workspace

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

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

# Or with yarn
yarn add @nrwl/react-native --dev

Generating an Application

To create additional React Native apps run:

npx nx g @nrwl/react-native:app

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 @nrwl/react-native:lib shared-ui-layout

And you will see the following:

happynrwl/
ā”œā”€ā”€ apps/
ā”‚   ā””ā”€ā”€ mobile/
ā”‚   ā””ā”€ā”€ mobile-e2e/
ā”œā”€ā”€ libs/
ā”‚   ā””ā”€ā”€ shared-ui-layout/
ā”‚       ā”œā”€ā”€ src/
ā”‚       ā”‚   ā””ā”€ā”€ index.ts
ā”‚       ā”œā”€ā”€ .babelrc
ā”‚       ā”œā”€ā”€ jest.config.js
ā”‚       ā”œā”€ā”€ project.json
ā”‚       ā”œā”€ā”€ README.md
ā”‚       ā”œā”€ā”€ test-setup.ts
ā”‚       ā”œā”€ā”€ tsconfig.json
ā”‚       ā”œā”€ā”€ tsconfig.lib.json
ā”‚       ā””ā”€ā”€ tsconfig.spec.json
ā”œā”€ā”€ tools/
ā”œā”€ā”€ babel.config.json
ā”œā”€ā”€ jest.config.js
ā”œā”€ā”€ jest.preset.js
ā”œā”€ā”€ nx.json
ā”œā”€ā”€ package-lock.json
ā”œā”€ā”€ package.json
ā”œā”€ā”€ tsconfig.base.json
ā””ā”€ā”€ workspace.json

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 comopnent inside shared-ui-layout run:

npx nx g @nrwl/react-native:component layout --project=shared-ui-layout --export

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

happynrwl/
ā””ā”€ā”€ libs/
    ā””ā”€ā”€ shared-ui-layout/
        ā””ā”€ā”€ src/
            ā”œā”€ā”€ index.ts
            ā””ā”€ā”€ lib/
                 ā””ā”€ā”€ layout/
                     ā”œā”€ā”€ layout.tsx
                     ā””ā”€ā”€ layout.spec.tsx

Using Nx Library in your Application

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

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

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 @nrwl/react-native:lib shared-ui-layout --publishable --importPath=@happynrwl/ui-components
npx nx g @nrwl/react-native:component layout --project=shared-ui-layout --export

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

dist/libs/shared-ui-layout/
ā”œā”€ā”€ README.md
ā”œā”€ā”€ index.d.ts
ā”œā”€ā”€ lib/
ā”‚   ā””ā”€ā”€ layout/
ā”‚       ā””ā”€ā”€ layout.d.ts
ā”œā”€ā”€ package.json
ā”œā”€ā”€ shared-ui-layout.esm.css
ā”œā”€ā”€ shared-ui-layout.esm.js
ā”œā”€ā”€ shared-ui-layout.umd.css
ā””ā”€ā”€ shared-ui-layout.umd.js

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

Environment Variables

The workspace should installreact-native-config by default. To use environment variable, create a new .env file in the happynrwl/apps/mobile folder:

NX_BUILD_NUMBER=123

Then access variables defined there from your app:

1import Config from 'react-native-config';
2
3Config.NX_BUILD_NUMBER; // '123'

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.

Resources

Here are other resources that you may find useful to learn more about React Native and Nx.