Skip to content

Commit

Permalink
[18] Update ReactDOMClient docs (#4468)
Browse files Browse the repository at this point in the history
* [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
  • Loading branch information
rickhanlonii committed Mar 29, 2022
1 parent d37523e commit 9081a48
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 70 deletions.
4 changes: 2 additions & 2 deletions beta/src/pages/apis/reactdom.md
Original file line number Diff line number Diff line change
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
2 changes: 2 additions & 0 deletions content/docs/nav.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
86 changes: 86 additions & 0 deletions content/docs/reference-react-dom-client.md
Original file line number Diff line number Diff line change
@@ -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.
110 changes: 42 additions & 68 deletions content/docs/reference-react-dom.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,95 +6,65 @@ category: Reference
permalink: docs/react-dom.html
---

If you load React from a `<script>` tag, these top-level APIs are available on the `ReactDOM` global. If you use ES6 with npm, you can write `import ReactDOM from 'react-dom'`. If you use ES5 with npm, you can write `var ReactDOM = require('react-dom')`.
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 the React model if you need to.

## Overview {#overview}

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.

- [`createRoot()`](#createroot)
- [`hydrateRoot()`](#hydrateroot)
- [`createPortal()`](#createportal)

> Note
>
> 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)):
>- [`render()`](#render)
>- [`hydrate()`](#hydrate)
>- [`findDOMNode()`](#finddomnode)
>- [`unmountComponentAtNode()`](#unmountcomponentatnode)
### Browser Support {#browser-support}

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.

> Note
>
> 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.
## Reference {#reference}

### `createRoot()` {#createroot}

```javascript
ReactDOM.createRoot(container[, options]);
```js
import * as ReactDOM from 'react-dom';
```

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`:
If you use ES5 with npm, you can write:

```javascript
const root = ReactDOM.createRoot(container);
root.render(element);
```js
var ReactDOM = require('react-dom');
```

The root can also be unmounted with `unmount`:

```javascript
root.unmount();
```
The `react-dom` package also provides modules specific to client and server apps:
- [`react-dom/client`](/docs/react-dom-client.html)
- [`react-dom/server`](/docs/react-dom-server.html)

> Note:
>
> `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.
>
> `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.
>
> Using `ReactDOM.createRoot()` to hydrate a server-rendered container is not supported. Use [`hydrateRoot()`](#hydrateroot) instead.
## Overview {#overview}

* * *
The `react-dom` package exports these methods:
- [`createPortal()`](#createportal)

### `hydrateRoot()` {#hydrateroot}

```javascript
ReactDOM.hydrateRoot(element, container[, options])
```
These `react-dom` methods are also exported, but are considered legacy:
- [`render()`](#render)
- [`hydrate()`](#hydrate)
- [`findDOMNode()`](#finddomnode)
- [`unmountComponentAtNode()`](#unmountcomponentatnode)

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.
> Note:
>
> Both `render` and `hydrate` have been replaced with new [client methods](/docs/react-dom-client.html) in React 18. These methods will warn that your app will behave as if it's running React 17 (learn more [here](https://reactjs.org/link/switch-to-createroot)).
`hydrateRoot` accepts two optional callbacks as options:
- `onDeleted`: callback called when content is deleted.
- `onHydrated`: callback called after hydration completes.
### Browser Support {#browser-support}

React supports all modern browsers, although [some polyfills are required](/docs/javascript-environment-requirements.html) for older versions.

> 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.
>
> 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}

### `createPortal()` {#createportal}

```javascript
ReactDOM.createPortal(child, container)
createPortal(child, container)
```

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).
## Legacy Reference
### `render()` {#render}
```javascript
ReactDOM.render(element, container[, callback])
render(element, container[, callback])
```

> Note:
>
> `render` has been replaced with `createRoot` in React 18. See [createRoot](/docs/react-dom-client.html#createroot) for more info.
Render a React element into the DOM in the supplied `container` and return a [reference](/docs/more-about-refs.html) to the component (or returns `null` for [stateless components](/docs/components-and-props.html#function-and-class-components)).

If the React element was previously rendered into `container`, this will perform an update on it and only mutate the DOM as necessary to reflect the latest React element.
Expand All @@ -103,24 +73,28 @@ If the optional callback is provided, it will be executed after the component is

> Note:
>
> `ReactDOM.render()` controls the contents of the container node you pass in. Any existing DOM elements inside are replaced when first called. Later calls use React’s DOM diffing algorithm for efficient updates.
> `render()` controls the contents of the container node you pass in. Any existing DOM elements inside are replaced when first called. Later calls use React’s DOM diffing algorithm for efficient updates.
>
> `ReactDOM.render()` 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.
> `render()` 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.
>
> `ReactDOM.render()` currently returns a reference to the root `ReactComponent` instance. However, using this return value is legacy
> `render()` currently returns a reference to the root `ReactComponent` instance. However, using this return value is legacy
> 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
> [callback ref](/docs/refs-and-the-dom.html#callback-refs) to the root element.
>
> Using `ReactDOM.render()` to hydrate a server-rendered container is deprecated. Use [`hydrateRoot()`](#hydrateroot) instead.
> Using `render()` to hydrate a server-rendered container is deprecated. Use [`hydrateRoot()`](#hydrateroot) instead.
* * *

### `hydrate()` {#hydrate}

```javascript
ReactDOM.hydrate(element, container[, callback])
hydrate(element, container[, callback])
```

> Note:
>
> `hydrate` has been replaced with `hydrateRoot` in React 18. See [hydrateRoot](/docs/react-dom-client.html#hydrateroot) for more info.
Same as [`render()`](#render), 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.

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.
Expand All @@ -136,7 +110,7 @@ Remember to be mindful of user experience on slow connections. The JavaScript co
### `unmountComponentAtNode()` {#unmountcomponentatnode}

```javascript
ReactDOM.unmountComponentAtNode(container)
unmountComponentAtNode(container)
```

Remove a mounted React component from the DOM and clean up its event handlers and state. If no component was mounted in the container, calling this function does nothing. Returns `true` if a component was unmounted and `false` if there was no component to unmount.
Expand All @@ -150,7 +124,7 @@ Remove a mounted React component from the DOM and clean up its event handlers an
> `findDOMNode` is an escape hatch used to access the underlying DOM node. In most cases, use of this escape hatch is discouraged because it pierces the component abstraction. [It has been deprecated in `StrictMode`.](/docs/strict-mode.html#warning-about-deprecated-finddomnode-usage)
```javascript
ReactDOM.findDOMNode(component)
findDOMNode(component)
```
If this component has been mounted into the DOM, this returns the corresponding native browser DOM element. This method is useful for reading values out of the DOM, such as form field values and performing DOM measurements. **In most cases, you can attach a ref to the DOM node and avoid using `findDOMNode` at all.**

Expand Down

0 comments on commit 9081a48

Please sign in to comment.