Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React 18 #4499

Merged
merged 42 commits into from Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
d37523e
[18] ReactDOM reference to createRoot/hydrateRoot (#4340)
rickhanlonii Mar 16, 2022
9081a48
[18] Update ReactDOMClient docs (#4468)
rickhanlonii Mar 19, 2022
2610a05
[18] Upgrade homepage examples (#4469)
rickhanlonii Mar 19, 2022
4af46e5
[18] Switch code samples to createRoot (#4470)
rickhanlonii Mar 19, 2022
7a2f6aa
[18] Use hydrateRoot and root.unmount. (#4473)
rickhanlonii Mar 19, 2022
9066aa5
[18] Add docs for flushSync (#4474)
rickhanlonii Mar 20, 2022
a78fdc3
[18] Bump version to 18 (#4478)
rickhanlonii Mar 20, 2022
d086d49
[18] Update browser requirements (#4476)
rickhanlonii Mar 21, 2022
e845ad8
[18] Add stubs for new API references (#4477)
rickhanlonii Mar 21, 2022
833377c
[18] Redirect outdated Concurrent Mode docs (#4481)
rickhanlonii Mar 22, 2022
4e16cad
[18] Update versions page (#4482)
rickhanlonii Mar 22, 2022
23e6a68
[18] Update React.lazy docs (#4483)
rickhanlonii Mar 22, 2022
87f1916
[18] Add docs for useSyncExternalStore (#4487)
rickhanlonii Mar 23, 2022
dcb74b3
[18] Add docs for useInsertionEffect (#4486)
rickhanlonii Mar 24, 2022
6eaf29b
[18] Add docs for useId (#4488)
rickhanlonii Mar 24, 2022
d845129
Add Strict Effects to Strict Mode (#4362)
rickhanlonii Mar 27, 2022
cec816c
[18] Update docs for useEffect timing (#4498)
rickhanlonii Mar 28, 2022
8052c92
[18] Add docs for useDeferredValue (#4497)
rickhanlonii Mar 28, 2022
51c31bc
[18] Update suspense docs for unexpected fallbacks (#4500)
rickhanlonii Mar 28, 2022
4e37212
[18] Updated Suspense doc with behavior during SSR and Hydration (#4484)
salazarm Mar 28, 2022
5c7bb6b
[18] renderToPipeableStream doc (#4485)
salazarm Mar 28, 2022
e88c7af
Rename strict effects / unsafe effects to use the reusable state term…
sebmarkbage Mar 28, 2022
554dfee
Add draft of 18 release post
acdlite Mar 29, 2022
f0836a3
Add links to speaker Twitter profiles
acdlite Mar 29, 2022
c826289
[18] Update upgrade guide
rickhanlonii Mar 29, 2022
ba8384b
Fix typo in blog title
rickhanlonii Mar 29, 2022
0b14677
[18] Blog - add note for react native
rickhanlonii Mar 29, 2022
0b90be7
[18] Add changelog info to blog posts
rickhanlonii Mar 29, 2022
58c9043
Edit Suspense for data fetching section
acdlite Mar 29, 2022
a9c42d6
Update date
acdlite Mar 29, 2022
99573ae
[18] Add links
rickhanlonii Mar 29, 2022
b4495f0
Consistent title case
acdlite Mar 29, 2022
52b5e67
Update link to merged RFC
acdlite Mar 29, 2022
3d3ccaf
[18] Update APIs and links
rickhanlonii Mar 29, 2022
5c44cc9
[18] Add start/useTransition docs (#4479)
rickhanlonii Mar 29, 2022
0b1b41a
[18] Generate heading IDs
rickhanlonii Mar 29, 2022
07ac383
Add note about Strict Mode
acdlite Mar 29, 2022
99518da
Just frameworks
gaearon Mar 29, 2022
6026906
Reorder, fix content
gaearon Mar 29, 2022
b34311d
Typos
gaearon Mar 29, 2022
d64a024
Clarify Suspense frameworks section
acdlite Mar 29, 2022
031c4aa
Revert lost changes that happened when I undo-ed in my editor
acdlite Mar 29, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions beta/src/pages/apis/reactdom.md
Expand Up @@ -22,10 +22,10 @@ npm install react-dom

```js
// Importing a specific API:
import { render } from 'react-dom';
import { createRoot } from 'react-dom/client';

// Importing all APIs together:
import * as ReactDOM from 'react';
import * as ReactDOMClient from 'react-dom/client';
```

</PackageImport>
Expand Down
19 changes: 10 additions & 9 deletions beta/src/pages/learn/add-react-to-a-website.md
Expand Up @@ -74,11 +74,12 @@ function LikeButton() {

### Step 4: Add your React Component to the page {/*step-4-add-your-react-component-to-the-page*/}

Lastly, add two lines to the bottom of **like_button.js**. These two lines of code find the `<div>` you added to your HTML in the first step and then display the "Like" button React component inside of it.
Lastly, add three lines to the bottom of **like_button.js**. These three lines of code find the `<div>` you added to your HTML in the first step, create a React app with it, and then display the "Like" button React component inside of it.

```js
const domContainer = document.getElementById('component-goes-here');
ReactDOM.render(React.createElement(LikeButton), domContainer);
const root = ReactDOM.createRoot(domContainer);
root.render(React.createElement(LikeButton));
```

**Congratulations! You have just rendered your first React component to your website!**
Expand All @@ -88,21 +89,21 @@ ReactDOM.render(React.createElement(LikeButton), domContainer);

#### You can reuse components! {/*you-can-reuse-components*/}

You might want to display a React component in multiple places on the same HTML page. This is most useful while React-powered parts of the page are isolated from each other. You can do this by calling `ReactDOM.render()` multiple times with multiple container elements.
You might want to display a React component in multiple places on the same HTML page. This is most useful while React-powered parts of the page are isolated from each other. You can do this by calling `ReactDOM.createRoot()` multiple times with multiple container elements.

1. In **index.html**, add an additional container element `<div id="component-goes-here-too"></div>`.
2. In **like_button.js**, add an additional `ReactDOM.render()` for the new container element:

```js {6,7,8,9}
ReactDOM.render(
React.createElement(LikeButton),
const root1 = ReactDOM.createRoot(
document.getElementById('component-goes-here')
);
root1.render(React.createElement(LikeButton));

ReactDOM.render(
React.createElement(LikeButton),
const root2 = ReactDOM.createRoot(
document.getElementById('component-goes-here-too')
);
root2.render(React.createElement(LikeButton));
```

Check out [an example that displays the "Like" button three times and passes some data to it](https://gist.github.com/rachelnabors/c0ea05cc33fbe75ad9bbf78e9044d7f8)!
Expand Down Expand Up @@ -157,8 +158,8 @@ Now you can use JSX in any `<script>` tag by adding `type="text/babel"` attribut

```jsx {1}
<script type="text/babel">
ReactDOM.render(
<h1>Hello, world!</h1>, document.getElementById('root') );
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<h1>Hello, world!</h1>);
</script>
```

Expand Down
3 changes: 3 additions & 0 deletions content/authors.yml
Expand Up @@ -76,6 +76,9 @@ petehunt:
rachelnabors:
name: Rachel Nabors
url: https://twitter.com/rachelnabors
reactteam:
name: The React Team
url: https://reactjs.org/community/team.html
rickhanlonii:
name: Rick Hanlon
url: https://twitter.com/rickhanlonii
Expand Down
73 changes: 49 additions & 24 deletions content/blog/2022-03-08-react-18-upgrade-guide.md
@@ -1,29 +1,29 @@
---
title: "How to Upgrade to the React 18 Release Candidate"
title: "How to Upgrade to React 18"
author: [rickhanlonii]
---

Our next major version, React 18, is available today as a Release Candidate (RC). As we shared at [React Conf](https://reactjs.org/blog/2021/12/17/react-conf-2021-recap.html), React 18 introduces features powered by our new concurrent renderer, with a gradual adoption strategy for existing applications. In this post, we will guide you through the steps for upgrading to React 18.
As we shared in the [release post](/blog/2022/03/28/react-v18.html), React 18 introduces features powered by our new concurrent renderer, with a gradual adoption strategy for existing applications. In this post, we will guide you through the steps for upgrading to React 18.

If you'd like to help us test React 18, follow the steps in this upgrade guide and [report any issues](https://github.com/facebook/react/issues/new/choose) you encounter so we can fix them before the stable release.
Please [report any issues](https://github.com/facebook/react/issues/new/choose) you encounter while upgrading to React 18.

*Note for React Native users: React 18 will ship in React Native with the New React Native Architecture. For more information, see the [React Conf keynote here](https://www.youtube.com/watch?v=FZ0cG47msEk&t=1530s).*

## Installing
## Installing {#installing}

To install the latest React 18 RC, use the `@rc` tag:
To install the latest version of React:

```bash
npm install react@rc react-dom@rc
npm install react react-dom
```

Or if you’re using yarn:

```bash
yarn add react@rc react-dom@rc
yarn add react react-dom
```

## Updates to Client Rendering APIs
## Updates to Client Rendering APIs {#updates-to-client-rendering-apis}

When you first install React 18, you will see a warning in the console:

Expand Down Expand Up @@ -97,7 +97,7 @@ const root = hydrateRoot(container, <App tab="home" />);

For more information, see the [working group discussion here](https://github.com/reactwg/react-18/discussions/5).

## Updates to Server Rendering APIs
## Updates to Server Rendering APIs {#updates-to-server-rendering-apis}

In this release, we’re revamping our `react-dom/server` APIs to fully support Suspense on the server and Streaming SSR. As part of these changes, we're deprecating the old Node streaming API, which does not support incremental Suspense streaming on the server.

Expand All @@ -120,7 +120,7 @@ Finally, this API will continue to work for rendering e-mails:

For more information on the changes to server rendering APIs, see the working group post on [Upgrading to React 18 on the server](https://github.com/reactwg/react-18/discussions/22), a [deep dive on the new Suspense SSR Architecture](https://github.com/reactwg/react-18/discussions/37), and [Shaundai Person’s](https://twitter.com/shaundai) talk on [Streaming Server Rendering with Suspense](https://www.youtube.com/watch?v=pj5N-Khihgc) at React Conf 2021.

## Automatic Batching
## Automatic Batching {#automatic-batching}

React 18 adds out-of-the-box performance improvements by doing more batching by default. Batching is when React groups multiple state updates into a single re-render for better performance. Before React 18, we only batched updates inside React event handlers. Updates inside of promises, setTimeout, native event handlers, or any other event were not batched in React by default:

Expand Down Expand Up @@ -179,17 +179,16 @@ function handleClick() {

For more information, see the [Automatic batching deep dive](https://github.com/reactwg/react-18/discussions/21).

## New APIs for Libraries
## New APIs for Libraries {#new-apis-for-libraries}

In the React 18 Working Group we worked with library maintainers to create new APIs needed to support concurrent rendering for use cases specific to their use case in areas like styles, external stores, and accessibility. To support React 18, some libraries may need to switch to one of the following APIs:
In the React 18 Working Group we worked with library maintainers to create new APIs needed to support concurrent rendering for use cases specific to their use case in areas like styles, and external stores. To support React 18, some libraries may need to switch to one of the following APIs:

* `useId` is a new hook for generating unique IDs on both the client and server, while avoiding hydration mismatches. This solves an issue that already exists in React 17 and below, but it's even more important in React 18 because of how our streaming server renderer delivers HTML out-of-order. For more information see the [useId post in the working group](https://github.com/reactwg/react-18/discussions/111).
* `useSyncExternalStore` is a new hook that allows external stores to support concurrent reads by forcing updates to the store to be synchronous. This new API is recommended for any library that integrates with state external to React. For more information, see the [useSyncExternalStore overview post](https://github.com/reactwg/react-18/discussions/70) and [useSyncExternalStore API details](https://github.com/reactwg/react-18/discussions/86).
* `useInsertionEffect` is a new hook that allows CSS-in-JS libraries to address performance issues of injecting styles in render. Unless you've already built a CSS-in-JS library we don't expect you to ever use this. This hook will run after the DOM is mutated, but before layout effects read the new layout. This solves an issue that already exists in React 17 and below, but is even more important in React 18 because React yields to the browser during concurrent rendering, giving it a chance to recalculate layout. For more information, see the [Library Upgrade Guide for `<style>`](https://github.com/reactwg/react-18/discussions/110).

React 18 also introduces new APIs for concurrent rendering such as `startTransition` and `useDeferredValue`, which we will share more about in the upcoming stable release post.
React 18 also introduces new APIs for concurrent rendering such as `startTransition`, `useDeferredValue` and `useId`, which we share more about in the [release post](/blog/2022/03/28/react-v18.html).

## Updates to Strict Mode
## Updates to Strict Mode {#updates-to-strict-mode}

In the future, we'd like to add a feature that allows React to add and remove sections of the UI while preserving state. For example, when a user tabs away from a screen and back, React should be able to immediately show the previous screen. To do this, React would unmount and remount trees using the same component state as before.

Expand Down Expand Up @@ -219,9 +218,9 @@ With Strict Mode in React 18, React will simulate unmounting and remounting the
* Effect setup code runs
```

For more information, see the Working Group posts for [Adding Strict Effects to Strict Mode](https://github.com/reactwg/react-18/discussions/19) and [How to Support Strict Effects](https://github.com/reactwg/react-18/discussions/18).
For more information, see the Working Group posts for [Adding Reusable State to StrictMode](https://github.com/reactwg/react-18/discussions/19) and [How to support Reusable State in Effects](https://github.com/reactwg/react-18/discussions/18).

## Configuring Your Testing Environment
## Configuring Your Testing Environment {#configuring-your-testing-environment}

When you first update your tests to use `createRoot`, you may see this warning in your test console:

Expand All @@ -242,16 +241,42 @@ Eventually, we expect testing libraries will configure this for you automaticall

[More background on the the `act` testing API and related changes](https://github.com/reactwg/react-18/discussions/102) is available in the working group.

## Dropping Support for Internet Explorer
## Dropping Support for Internet Explorer {#dropping-support-for-internet-explorer}

In this release, React is dropping support for Internet Explorer, which is [going out of support on June 15, 2022](https://blogs.windows.com/windowsexperience/2021/05/19/the-future-of-internet-explorer-on-windows-10-is-in-microsoft-edge). We’re making this change now because new features introduced in React 18 are built using modern browser features such as microtasks which cannot be adequately polyfilled in IE.

If you need to support Internet Explorer we recommend you stay with React 17.

## Other Changes
## Deprecations {#deprecations}

* [Update to remove the "setState on unmounted component" warning](https://github.com/reactwg/react-18/discussions/82)
* [Suspense no longer requires a `fallback` prop to capture](https://github.com/reactwg/react-18/discussions/72)
* [Components can now render undefined](https://github.com/reactwg/react-18/discussions/75)
* [Deprecated renderSubtreeIntoContainer](https://github.com/facebook/react/pull/23355)
* [StrictMode updated to not silence double logging by default](https://github.com/reactwg/react-18/discussions/96)
* `react-dom`: `ReactDOM.render` has been deprecated. Using it will warn and run your app in React 17 mode.
* `react-dom`: `ReactDOM.hydrate` has been deprecated. Using it will warn and run your app in React 17 mode.
* `react-dom`: `ReactDOM.unmountComponentAtNode` has been deprecated.
* `react-dom`: `ReactDOM.renderSubtreeIntoContainer` has been deprecated.
* `react-dom/server`: `ReactDOMServer.renderToNodeStream` has been deprecated.

## Other Breaking Changes {#other-breaking-changes}

* **Consistent useEffect timing**: React now always synchronously flushes effect functions if the update was triggered during a discrete user input event such as a click or a keydown event. Previously, the behavior wasn't always predictable or consistent.
* **Stricter hydration errors**: Hydration mismatches due to missing or extra text content are now treated like errors instead of warnings. React will no longer attempt to "patch up" individual nodes by inserting or deleting a node on the client in an attempt to match the server markup, and will revert to client rendering up to the closest `<Suspense>` boundary in the tree. This ensures the hydrated tree is consistent and avoids potential privacy and security holes that can be caused by hydration mismatches.
* **Layout Effects with Suspense**: When a tree re-suspends and reverts to a fallback, React will now clean up layout effects, and then re-create them when the content inside the boundary is shown again. This fixes an issue which prevented component libraries from correctly measuring layout when used with Suspense.
* **New JS Environment Requirements**: React now depends on modern browsers features including `Promise`, `Symbol`, and `Object.assign`. If you support older browsers and devices such as Internet Explorer which do not provide modern browser features natively or have non-compliant implementations, consider including a global polyfill in your bundled application.

## Other Notable Changes {#other-notable-changes}

### React {#react}

* **Components can now render `undefined`:** React no longer warns if you return `undefined` from a component. This makes the allowed component return values consistent with values that are allowed in the middle of a component tree. We suggest to use a linter to prevent mistakes like forgetting a `return` statement before JSX.
* **In tests, `act` warnings are now opt-in:** If you're running end-to-end tests, the `act` warnings are unnecessary. We've introduced an [opt-in](https://github.com/reactwg/react-18/discussions/102) mechanism so you can enable them only for unit tests where they are useful and beneficial.
* **No warning about `setState` on unmounted components:** Previously, React warned about memory leaks when you call `setState` on an unmounted component. This warning was added for subscriptions, but people primarily run into it in scenarios where setting state is fine, and workarounds make the code worse. We've [removed](https://github.com/facebook/react/pull/22114) this warning.
* **No suppression of console logs:** When you use Strict Mode, React renders each component twice to help you find unexpected side effects. In React 17, we've suppressed console logs for one of the two renders to make the logs easier to read. In response to [community feedback](https://github.com/facebook/react/issues/21783) about this being confusing, we've removed the suppression. Instead, if you have React DevTools installed, the second log's renders will be displayed in grey, and there will be an option (off by default) to suppress them completely.
* **Improved memory usage:** React now cleans up more internal fields on unmount, making the impact from unfixed memory leaks that may exist in your application code less severe.

### React DOM Server {#react-dom-server}

* **`renderToString`:** Will no longer error when suspending on the server. Instead, it will emit the fallback HTML for the closest `<Suspense>` boundary and then retry rendering the same content on the client. It is still recommended that you switch to a streaming API like `renderToPipeableStream` or `renderToReadableStream` instead.
* **`renderToStaticMarkup`:** Will no longer error when suspending on the server. Instead, it will emit the fallback HTML for the closest `<Suspense>` boundary and retry rendering on the client.

## Changelog {#changelog}

You can view the [full changelog here](https://github.com/facebook/react/blob/main/CHANGELOG.md).