You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/docs/reference-react-dom.md
+66-15Lines changed: 66 additions & 15 deletions
Original file line number
Diff line number
Diff line change
@@ -12,12 +12,18 @@ If you load React from a `<script>` tag, these top-level APIs are available on t
12
12
13
13
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.
> 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)):
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
26
32
>
27
33
> 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.
28
34
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
+
constroot=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.
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
+
```
34
90
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}
35
94
```javascript
36
95
ReactDOM.render(element, container[, callback])
37
96
```
@@ -52,7 +111,7 @@ If the optional callback is provided, it will be executed after the component is
52
111
> 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
53
112
> [callback ref](/docs/refs-and-the-dom.html#callback-refs) to the root element.
54
113
>
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.
56
115
57
116
* * *
58
117
@@ -104,11 +163,3 @@ When a component renders to `null` or `false`, `findDOMNode` returns `null`. Whe
104
163
> `findDOMNode` cannot be used on function components.
105
164
106
165
* * *
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