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 likeDetox, Storybook, Jest, and more.
In this guide we will show you how to developReact Native applications with Nx.
Creating Nx Workspace
The easiest way to create your workspace is vianpx
.
❯
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.json5│ │ ├── metro.config.js6│ │ ├── android/7│ │ │ ├── app/8│ │ │ ├── gradle/9│ │ │ ├── build.gradle10│ │ │ ├── gradle.properties11│ │ │ ├── gradlew12│ │ │ ├── settings.gradle13│ │ ├── ios/14│ │ │ ├── Mobile/15│ │ │ ├── Mobile.xcodeproj/16│ │ │ ├── Mobile.xcworkspace/17│ │ │ ├── Podfile18│ │ │ ├── Podfile.lock19│ │ ├── src/20│ │ │ ├── main.tsx21│ │ │ └── app/22│ │ │ ├── App.tsx23│ │ │ └── App.spec.tsx24│ │ ├── .babelrc25│ │ ├── jest.config.ts26│ │ ├── test-setup.ts27│ │ ├── package.json28│ │ ├── project.json29│ │ ├── tsconfig.json30│ │ ├── tsconfig.app.json31│ │ └── tsconfig.spec.json32│ └── mobile-e2e/33│ ├── .detoxrc.json34│ ├── src/35│ │ └── app.spec.ts36│ ├── .babelrc37│ ├── jest.config.json38│ ├── project.json39│ ├── tsconfig.e2e.json40│ └── tsconfig.json41├── libs/42├── tools/43├── babel.config.json44├── jest.config.ts45├── jest.preset.js46├── nx.json47├── package-lock.json48├── package.json49└── tsconfig.base.json50
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 theInferred 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 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 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.ts9│ ├── .babelrc10│ ├── jest.config.js11│ ├── project.json12│ ├── README.md13│ ├── test-setup.ts14│ ├── tsconfig.json15│ ├── tsconfig.lib.json16│ └── tsconfig.spec.json17├── tools/18├── babel.config.json19├── jest.config.js20├── jest.preset.js21├── nx.json22├── package-lock.json23├── package.json24└── tsconfig.base.json25
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 insideshared-ui-layout
run:
❯
npx nx g @nx/react-native:component libs/shared-ui-layout/src/lib/layout/layout --export
And you will see the following updated forshared-ui-layout
:
1happynrwl/2└── libs/3 └──shared-ui-layout/4 └── src/5 ├──index.ts6 └── lib/7 └──layout/8 ├──layout.tsx9 └──layout.spec.tsx10
Using Nx Library in your Application
You can import theshared-ui-layout
library in your application as follows.
1import Reactfrom'react';2import { SafeAreaView }from'react-native';34import { Layout }from'@happynrwl/shared-ui-layout';56const App =() => {7return (8<SafeAreaView>9<Layout />10</SafeAreaView>11 );12};1314exportdefault 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 libs/shared-ui-layout --publishable --importPath=@happynrwl/ui-components
❯
npx nx g @nx/react-native:component libs/shared-ui-layout/src/lib/layout/layout --export
Runnpx nx build shared-ui-layout
to build the library. It will generate the following:
1dist/libs/shared-ui-layout/2├── README.md3├── index.d.ts4├── lib/5│ └── layout/6│ └── layout.d.ts7└── package.json8
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.