This PR updates examples in `.md` files (both docs and blog posts) to use positional args. Nx 20 changes the position arg to be either `directory` for apps/libs or `path` for artifacts (e.g. components). So before you'd do this: ``` nx g app myapp --directory=apps/myapp nx g lib mylib --directory=libs/mylib nx g lib mylib --directory=libs/nested/mylib nx g lib @acme/foo --directory=libs/@acme/foo --importPath=@acme/foo nx g component foo --directory=libs/ui/src/foo --pascalCaseFiles ``` Will now be simplified to ``` nx g app apps/myapp nx g lib libs/mylib nx g lib libs/nested/mylib nx g lib libs/@acme/foo # name and import path are both "@acme/foo" nx g component libs/ui/src/foo/Foo ``` For cases where `name` and `importPath` need to be changed, you can always manually specify them. ``` nx g lib libs/nested/foo # name is foo nx g lib libs/nested/foo --name=nested-foo # specify name with prefix nx g lib libs/@acme/foo --name # use "foo" as name and don't match importPath nx g lib libs/@internal/foo --importPath=@acme/foo # different importPath from name <!-- If this is a particularly complex change or feature addition, you can request a dedicated Nx release for this pull request branch. Mention someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they will confirm if the PR warrants its own release for testing purposes, and generate it for you if appropriate. --> ## Current Behavior <!-- This is the behavior we have today --> ## Expected Behavior <!-- This is the behavior we should expect with the changes in this PR --> ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes #
7.5 KiB
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
{% callout type="note" title="Don't know what you need?" %} You can also run the command without arguments to go through the interactive prompts. {% /callout %}
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.ts
│ │ ├── 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.ts
├── jest.preset.js
├── nx.json
├── package-lock.json
├── package.json
└── tsconfig.base.json
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 mobileto lint the applicationnx test mobileto run unit test on the application using Jestnx sync-deps mobileto sync app dependencies to itspackage.json.
Release build
Android:
npx nx build-android mobile
iOS: (Mac only)
npx nx build-ios mobile
E2E
Android:
{% tabs %} {% tab label="Using inferred tasks" %}
{% callout type="note" title="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. {% /callout %}
npx nx test mobile-e2e -- --configuration="android.emu.debug"
{% /tab %} {% tab label="Using explicit targets with executors" %}
npx nx test-android mobile-e2e
{% /tab %}
iOS: (Mac only)
{% tabs %} {% tab label="Using inferred tasks" %}
npx nx test mobile-e2e -- --configuration="ios.sim.debug"
{% /tab %} {% tab label="Using explicit targets with executors" %}
npx nx test-ios mobile-e2e
{% /tab %}
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:
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
Run:
npx nx test shared-ui-layoutto test the librarynpx nx lint shared-ui-layoutto lint the library
To generate a new component inside shared-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 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.
import React from 'react';
import { SafeAreaView } from 'react-native';
import { Layout } from '@happynrwl/shared-ui-layout';
const App = () => {
return (
<SafeAreaView>
<Layout />
</SafeAreaView>
);
};
export 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 @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
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
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.