React 18 Migration

React 18 released with many new features, such as Concurrent React, Suspense, batched updates, and more.

Workspaces that upgrade to @nrwl/react 14 will be automatically migrated to React 18. This migration will also include an upgrade to React Router v6, if it is used in the workspace, as well as the removal of the deprecated @testing-library/react-hook package. Keep reading for more details.

Note: If you use npm v7/v8, you will need to use npm install --force after running nx migrate 14.0.0 since @testing-library/react-hook does not support React 18. Don't worry, this package will be removed in the migration.

New react-dom/client API

Nx will automatically update your applications to use the new react-dom-/client API.

From this:

1import { StrictMode } from 'react';
2import * as ReactDOM from 'react-dom';
3import App from './app/app';
4
5ReactDOM.render(
6  <StrictMode>
7    <App />
8  </StrictMode>,
9  document.getElementById('root')
10);

To this:

1import { StrictMode } from 'react';
2import * as ReactDOM from 'react-dom/client';
3import App from './app/app';
4
5const root = ReactDOM.createRoot(
6  document.getElementById('root') as HTMLElement
7);
8root.render(
9  <StrictMode>
10    <App />
11  </StrictMode>
12);

There might be additional changes needed for your code to be fully compatible with React 18. If you use React.FC type (which Nx does not use), then you will need to update your component props to include children explicitly.

Before:

1interface MyButtonProps {
2  color: string;
3}

After:

1interface MyButtonProps {
2  color: string;
3  children?: React.ReactNode; // children is no longer implicitly provided by React.FC
4}

For more information on React 18 migration, please see the official guide.

Changes to Strict Mode

React 18 brings a change to Strict Mode that requires effects to be resilient to mounting and unmounting multiple times.

This change means that in development mode, React will simulate mounting and unmounting an effect, even though the component using the effect only mounts once. Note that this does not affect production.

In practice, this change means that if you use an effect without dependencies, such as the following.

1useEffect(() => {
2  console.log('running effect');
3  return () => {
4    console.log('clean up');
5  };
6}, []);

Then, in React 18 with Strict Mode, you'll see the following logged:

running effect
clean up
running effect

This behavior is problematic if the effect cannot run twice, say if you fetch data or perform expensive computation. To fix it, you can use a ref.

1const hasRun = useRef(false);
2
3useEffect(() => {
4  if (!hasRun.current) {
5    hasRun.current = true;
6    console.log('running effect');
7  }
8  return () => {
9    console.log('clean up');
10  };
11}, []);

Alternatively, you can switch Strict Mode off, which might be a good temporary solution until you are able to fix all the problematic effects in your workspace. To turn Strict Mode off, delete the <Strict> element the application's main.tsx. If you are using Next.js, you can use the reactStrictMode setting in your next.config.js file.

React Router v6

In addition to the React 18 migration, Nx will also update your workspace to React Router v6 -- assuming you use React Router v5 previously. There are breaking changes in React Router v6. Please refer to the official v5 to v6 guide for details.

We highly recommend teams to upgrade their workspace to v6, but if you choose to opt out and continue to use v5, then you will need to disable React strict mode. Navigation is broken in strict mode for React Router v5 due to a transition issue.

To disable strict mode, open your main.tsx file and remove <Strict> in your render function.

Before:

1root.render(
2  <Strict>
3    <BrowserRouter>
4      <App />
5    </BrowserRouter>
6  </Strict>
7);

After (for React Router v5):

1root.render(
2  <BrowserRouter>
3    <App />
4  </BrowserRouter>
5);

@testing-library/react-hook is deprecated

The @testing-library/react-hook package provides a renderHook function to test custom hooks. Unfortunately, this package does not support React 18, and has been deprecated. The good news is that @testing-library/react (RTL) now comes with its own renderHook utility function since version 13.1.0.

Nx will migrate your code to import renderHook from @testing-library/react instead of the deprecated package. There are a couple of utility functions missing from the RTL package: waitForNextUpdate and waitForValueToChange. If you use either of these utility functions, try swapping them with waitFor instead.

If you continue to have issues after the migration, please open an issue on the RTL repo: https://github.com/testing-library/react-testing-library.

React Native v0.68

Nx will also update your workspace to React Native v0.68. There are breaking changes, please refer to the official React Native 0.68 guide for details.

Because typical React Native projects are essentially made up of a JavaScript project and native code projects (an Android project, an iOS project). For native code, you can either upgrade manually using [React Native Upgrade Helper](https://react-native-community.github.io/upgrade-helper/ or using the @nrwl/react-native:upgrade-native generator.

For manual update, go to React Native Upgrade Helper and copy the diff to your native code one by one. Nevertheless, you can use Nx's command nx g @nrwl/react-native:upgrade-native <your project name> to replace your iOS and Android folder with the latest code. (Note: this command is destructive, do NOT use it if you got customized native code.)