-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(react): upgrade to react-router v6
BREAKING CHANGE: react-router API has changed (See migration guide: https://reactrouter.com/docs/en/v6/upgrading/v5)
- Loading branch information
Showing
12 changed files
with
349 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# React 18 Migration | ||
|
||
Workspaces that upgrade to Nx 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. | ||
|
||
Nx will automatically update your applications to use the new `react-dom-/client` API. | ||
|
||
From this: | ||
|
||
```typescript jsx | ||
import { StrictMode } from 'react'; | ||
import * as ReactDOM from 'react-dom'; | ||
import App from './app/app'; | ||
|
||
ReactDOM.render( | ||
<StrictMode> | ||
<App /> | ||
</StrictMode>, | ||
document.getElementById('root') | ||
); | ||
``` | ||
|
||
To this: | ||
|
||
```typescript jsx | ||
import { StrictMode } from 'react'; | ||
import * as ReactDOM from 'react-dom/client'; | ||
import App from './app/app'; | ||
|
||
const root = ReactDOM.createRoot( | ||
document.getElementById('root') as HTMLElement | ||
); | ||
root.render( | ||
<StrictMode> | ||
<App /> | ||
</StrictMode> | ||
); | ||
``` | ||
|
||
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: | ||
|
||
```typescript jsx | ||
interface MyButtonProps { | ||
color: string; | ||
} | ||
``` | ||
|
||
After: | ||
|
||
```typescript jsx | ||
interface MyButtonProps { | ||
color: string; | ||
children?: React.ReactNode; // children is no longer implicitly provided by React.FC | ||
} | ||
``` | ||
|
||
For more information on React 18 migration, please see the [official guide](https://reactjs.org/blog/2022/03/08/react-18-upgrade-guide.html). | ||
|
||
## 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](https://reactrouter.com/docs/en/v6/upgrading/v5) 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: | ||
|
||
```typescript jsx | ||
root.render( | ||
<Strict> | ||
<BrowserRouter> | ||
<App /> | ||
</BrowserRouter> | ||
</Strict> | ||
); | ||
``` | ||
|
||
After (for React Router v5): | ||
|
||
```typescript jsx | ||
root.render( | ||
<BrowserRouter> | ||
<App /> | ||
</BrowserRouter> | ||
); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
packages/react/src/generators/application/files/common/src/main.tsx__tmpl__
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
<% if (strict) { %>import { StrictMode } from 'react';<% } %> | ||
import * as ReactDOMClient from 'react-dom/client'; | ||
import * as ReactDOM from 'react-dom/client'; | ||
<% if (routing) { %>import { BrowserRouter } from 'react-router-dom';<% } %> | ||
|
||
import App from './app/<%= fileName %>'; | ||
|
||
const root = ReactDOMClient.createRoot(document.getElementById('root') as HTMLElement); | ||
const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement); | ||
root.render( | ||
<% if (strict) { %><StrictMode><% } %><% if (routing) { %><BrowserRouter><% } %><App /><% if (routing) { %></BrowserRouter><% } %><% if (strict) { %></StrictMode><% } %> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { logger, Tree } from '@nrwl/devkit'; | ||
|
||
export async function update(tree: Tree) { | ||
logger.warn( | ||
`Nx 14 comes with React Router v6, which breaks backward compatibility. For more information, refer to the guide here: https://nx.dev/guides/react-18#react-router-v6.` | ||
); | ||
} | ||
|
||
export default update; |
Oops, something went wrong.