From 9081a485db713cbe9cf1451752078dc4cae22999 Mon Sep 17 00:00:00 2001 From: Ricky Date: Sat, 19 Mar 2022 12:45:17 -0400 Subject: [PATCH] [18] Update ReactDOMClient docs (#4468) * [18] Update ReactDOMClient docs * Remove ReactDOMClient where it's obvious * Update browser message * Update browser message note * Update based on feedback * Add react-dom/client docs --- beta/src/pages/apis/reactdom.md | 4 +- content/docs/nav.yml | 2 + content/docs/reference-react-dom-client.md | 86 ++++++++++++++++ content/docs/reference-react-dom.md | 110 ++++++++------------- 4 files changed, 132 insertions(+), 70 deletions(-) create mode 100644 content/docs/reference-react-dom-client.md diff --git a/beta/src/pages/apis/reactdom.md b/beta/src/pages/apis/reactdom.md index ad450f965e8..229cb369d10 100644 --- a/beta/src/pages/apis/reactdom.md +++ b/beta/src/pages/apis/reactdom.md @@ -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'; ``` diff --git a/content/docs/nav.yml b/content/docs/nav.yml index dbedc5f5f38..d0acf873b96 100644 --- a/content/docs/nav.yml +++ b/content/docs/nav.yml @@ -92,6 +92,8 @@ title: React.Component - id: react-dom title: ReactDOM + - id: react-dom-client + title: ReactDOMClient - id: react-dom-server title: ReactDOMServer - id: dom-elements diff --git a/content/docs/reference-react-dom-client.md b/content/docs/reference-react-dom-client.md new file mode 100644 index 00000000000..dfd4bcbd526 --- /dev/null +++ b/content/docs/reference-react-dom-client.md @@ -0,0 +1,86 @@ +--- +id: react-dom-client +title: ReactDOMClient +layout: docs +category: Reference +permalink: docs/react-dom-client.html +--- + +The `react-dom/client` package provides client-specific methods used for initializing an app on the client. Most of your components should not need to use this module. + +```js +import * as ReactDOM from 'react-dom/client'; +``` + +If you use ES5 with npm, you can write: + +```js +var ReactDOM = require('react-dom/client'); +``` + +## Overview {#overview} + +The following methods can be used in client environments: + +- [`createRoot()`](#createroot) +- [`hydrateRoot()`](#hydrateroot) + +### Browser Support {#browser-support} + +React supports all modern browsers, although [some polyfills are required](/docs/javascript-environment-requirements.html) for older versions. + +> Note +> +> We do not support older browsers that don't support ES5 methods or microtasks such as Internet Explorer. You may find that your apps do work in older browsers if polyfills such as [es5-shim and es5-sham](https://github.com/es-shims/es5-shim) are included in the page, but you're on your own if you choose to take this path. + +## Reference {#reference} + +### `createRoot()` {#createroot} + +```javascript +createRoot(container[, options]); +``` + +Create a React root for the supplied `container` and return the root. The root can be used to render a React element into the DOM with `render`: + +```javascript +const root = createRoot(container); +root.render(element); +``` + +`createRoot` accepts two options: +- `onRecoverableError`: optional callback called when React automatically recovers from errors. +- `identifierPrefix`: optional prefix React uses for ids generated by `React.useId`. Useful to avoid conflicts when using multiple roots on the same page. Must be the same prefix used on the server. + +The root can also be unmounted with `unmount`: + +```javascript +root.unmount(); +``` + +> Note: +> +> `createRoot()` controls the contents of the container node you pass in. Any existing DOM elements inside are replaced when render is called. Later calls use React’s DOM diffing algorithm for efficient updates. +> +> `createRoot()` does not modify the container node (only modifies the children of the container). It may be possible to insert a component to an existing DOM node without overwriting the existing children. +> +> Using `createRoot()` to hydrate a server-rendered container is not supported. Use [`hydrateRoot()`](#hydrateroot) instead. + +* * * + +### `hydrateRoot()` {#hydrateroot} + +```javascript +hydrateRoot(element, container[, options]) +``` + +Same as [`createRoot()`](#createroot), but is used to hydrate a container whose HTML contents were rendered by [`ReactDOMServer`](/docs/react-dom-server.html). React will attempt to attach event listeners to the existing markup. + +`hydrateRoot` accepts two options: +- `onRecoverableError`: optional callback called when React automatically recovers from errors. +- `identifierPrefix`: optional prefix React uses for ids generated by `React.useId`. Useful to avoid conflicts when using multiple roots on the same page. Must be the same prefix used on the server. + + +> Note +> +> React expects that the rendered content is identical between the server and the client. It can patch up differences in text content, but you should treat mismatches as bugs and fix them. In development mode, React warns about mismatches during hydration. There are no guarantees that attribute differences will be patched up in case of mismatches. This is important for performance reasons because in most apps, mismatches are rare, and so validating all markup would be prohibitively expensive. diff --git a/content/docs/reference-react-dom.md b/content/docs/reference-react-dom.md index ac36022d6b4..e6e1c55175c 100644 --- a/content/docs/reference-react-dom.md +++ b/content/docs/reference-react-dom.md @@ -6,95 +6,65 @@ category: Reference permalink: docs/react-dom.html --- -If you load React from a `