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
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 applicationnx test mobile
to run unit test on the application using Jestnx sync-deps mobile
to sync app dependencies to itspackage.json
.
Release build
Android:
โฏ
npx nx build-android mobile
iOS: (Mac only)
โฏ
npx nx build-ios mobile
E2E
Android:
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
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
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 librarynpx 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.
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.