Skip to content

Commit d37523e

Browse files
committed
[18] ReactDOM reference to createRoot/hydrateRoot (#4340)
* [18] ReactDOM reference to createRoot/hydrateRoot * Update note about render and hydrate * Match the warning text * s/Render/render
1 parent ac68ced commit d37523e

File tree

1 file changed

+66
-15
lines changed

1 file changed

+66
-15
lines changed

content/docs/reference-react-dom.md

Lines changed: 66 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,18 @@ If you load React from a `<script>` tag, these top-level APIs are available on t
1212

1313
The `react-dom` package provides DOM-specific methods that can be used at the top level of your app and as an escape hatch to get outside of the React model if you need to. Most of your components should not need to use this module.
1414

15-
- [`render()`](#render)
16-
- [`hydrate()`](#hydrate)
17-
- [`unmountComponentAtNode()`](#unmountcomponentatnode)
18-
- [`findDOMNode()`](#finddomnode)
15+
- [`createRoot()`](#createroot)
16+
- [`hydrateRoot()`](#hydrateroot)
1917
- [`createPortal()`](#createportal)
2018

19+
> Note
20+
>
21+
> These methods are considered legacy. Both `render` and `hydrate` will warn that your app will behave as if it's running React 17 (learn more [here](https://reactjs.org/link/switch-to-createroot)):
22+
>- [`render()`](#render)
23+
>- [`hydrate()`](#hydrate)
24+
>- [`findDOMNode()`](#finddomnode)
25+
>- [`unmountComponentAtNode()`](#unmountcomponentatnode)
26+
2127
### Browser Support {#browser-support}
2228

2329
React supports all popular browsers, including Internet Explorer 9 and above, although [some polyfills are required](/docs/javascript-environment-requirements.html) for older browsers such as IE 9 and IE 10.
@@ -26,12 +32,65 @@ React supports all popular browsers, including Internet Explorer 9 and above, al
2632
>
2733
> We don't support older browsers that don't support ES5 methods, but 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. You're on your own if you choose to take this path.
2834
35+
## Reference {#reference}
36+
37+
### `createRoot()` {#createroot}
38+
39+
```javascript
40+
ReactDOM.createRoot(container[, options]);
41+
```
42+
43+
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`:
44+
45+
```javascript
46+
const root = ReactDOM.createRoot(container);
47+
root.render(element);
48+
```
49+
50+
The root can also be unmounted with `unmount`:
51+
52+
```javascript
53+
root.unmount();
54+
```
55+
56+
> Note:
57+
>
58+
> `ReactDOM.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.
59+
>
60+
> `ReactDOM.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.
61+
>
62+
> Using `ReactDOM.createRoot()` to hydrate a server-rendered container is not supported. Use [`hydrateRoot()`](#hydrateroot) instead.
63+
2964
* * *
3065

31-
## Reference {#reference}
66+
### `hydrateRoot()` {#hydrateroot}
3267

33-
### `render()` {#render}
68+
```javascript
69+
ReactDOM.hydrateRoot(element, container[, options])
70+
```
71+
72+
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.
73+
74+
`hydrateRoot` accepts two optional callbacks as options:
75+
- `onDeleted`: callback called when content is deleted.
76+
- `onHydrated`: callback called after hydration completes.
77+
78+
79+
> Note
80+
>
81+
> 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.
82+
83+
* * *
84+
85+
### `createPortal()` {#createportal}
86+
87+
```javascript
88+
ReactDOM.createPortal(child, container)
89+
```
3490

91+
Creates a portal. Portals provide a way to [render children into a DOM node that exists outside the hierarchy of the DOM component](/docs/portals.html).
92+
## Legacy Reference
93+
### `render()` {#render}
3594
```javascript
3695
ReactDOM.render(element, container[, callback])
3796
```
@@ -52,7 +111,7 @@ If the optional callback is provided, it will be executed after the component is
52111
> and should be avoided because future versions of React may render components asynchronously in some cases. If you need a reference to the root `ReactComponent` instance, the preferred solution is to attach a
53112
> [callback ref](/docs/refs-and-the-dom.html#callback-refs) to the root element.
54113
>
55-
> Using `ReactDOM.render()` to hydrate a server-rendered container is deprecated and will be removed in React 17. Use [`hydrate()`](#hydrate) instead.
114+
> Using `ReactDOM.render()` to hydrate a server-rendered container is deprecated. Use [`hydrateRoot()`](#hydrateroot) instead.
56115
57116
* * *
58117

@@ -104,11 +163,3 @@ When a component renders to `null` or `false`, `findDOMNode` returns `null`. Whe
104163
> `findDOMNode` cannot be used on function components.
105164
106165
* * *
107-
108-
### `createPortal()` {#createportal}
109-
110-
```javascript
111-
ReactDOM.createPortal(child, container)
112-
```
113-
114-
Creates a portal. Portals provide a way to [render children into a DOM node that exists outside the hierarchy of the DOM component](/docs/portals.html).

0 commit comments

Comments
 (0)