Browse Source

[Beta] Change render() API page format (#4319)

* [Beta] Change render() API page format

* Tweaks
main
dan 3 years ago
committed by GitHub
parent
commit
4f764b26da
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 106
      beta/src/pages/apis/render.md

106
beta/src/pages/apis/render.md

@ -4,36 +4,96 @@ title: render()
<Intro>
`render` renders a piece of [JSX](/learn/writing-markup-with-jsx) ("React element") into a browser DOM container node. It instructs React to change the DOM inside of the `container` so that it matches the passed JSX.
`render` renders a piece of [JSX](/learn/writing-markup-with-jsx) ("React node") into a browser DOM node.
</Intro>
## On this page {/*on-this-page*/}
- [Reference](#reference)
- [`render(reactNode, domNode)`](#render)
- [`render(reactNode, domNode, callback)`](#render-callback)
- [Usage](#usage)
- [Rendering the root component](#rendering-the-root-component)
- [Rendering multiple roots](#rendering-multiple-roots)
- [Updating the rendered tree](#updating-the-rendered-tree)
## Reference {/*reference*/}
### `render(reactNode, domNode)` {/*render*/}
Call `render` to display a React component inside a browser DOM element.
```js
render(<App />, container);
render(<App />, container, callback);
const domNode = document.getElementById('root');
render(<App />, domNode);
```
</Intro>
React will display `<App />` in the `domNode`, and take over managing the DOM inside it.
An app fully built with React will usually only have one `render` call with its root component. A page that uses "sprinkles" of React for parts of the page may have as many `render` calls as needed.
[See examples below.](#usage)
#### Parameters {/*parameters*/}
* `reactNode`: A *React node* that you want to display. This will usually be a piece of JSX like `<App />`, but you can also pass a React elmenent constructed with [`createElement()`](/TODO), a string, a number, `null`, or `undefined`.
* `domNode`: A [DOM element](https://developer.mozilla.org/en-US/docs/Web/API/Element). React will display the `reactNode` you pass inside this DOM element. From this moment, React will manage the DOM inside the `domNode` and update it when your React tree changes.
#### Returns {/*returns*/}
`render` usually returns `null`. However, if the `reactNode` you pass is a *class component*, then it will return an instance of that component.
#### Caveats {/*caveats*/}
* The first time you call `render`, React will clear all the existing HTML content inside the `domNode` before rendering the React component into it. If your `domNode` contains HTML generated by React on the server or during the build, use [`hydrate()`](/TODO) instead, which attaches the event handlers to the existing HTML.
* If you call `render` on the same `domNode` more than once, React will update the DOM as necessary to reflect the latest JSX you passed. React will decide which parts of the DOM can be reused and which need to be recreated by ["matching it up"](/learn/preserving-and-resetting-state) with the previously rendered tree. Calling `render` on the same `domNode` again is similar to calling the [`set` function](/apis/usestate#setstate) on the root component: React avoids unnecessary DOM updates.
* If your app is fully built with React, you'll likely have only one `render` call in your app. (If you use a framework, it might do this call for you.) When you want to render a piece of JSX in a different part of the DOM tree that isn't a child of your component (for example, a modal or a tooltip), use [`createPortal`](TODO) instead of `render`.
---
## Rendering the root component {/*rendering-the-root-component*/}
### `render(reactNode, domNode, callback)` {/*render-callback*/}
Same as [`render(reactNode, domNode)`](#render), but with a `callback` that notifies you when your component has been placed into the DOM.
#### Parameters {/*render-callback-parameters*/}
* `reactNode`: [Same as above.](#parameters)
* `domNode`: [Same as above.](#parameters)
* `callback`: A function. If passed, React will call it after your component is placed into the DOM.
#### Returns {/*render-callback-returns*/}
[Same as above.](#returns)
---
## Usage {/*usage*/}
### Rendering the root component {/*rendering-the-root-component*/}
To call `render`, you need a piece of JSX and a DOM container:
<APIAnatomy>
<AnatomyStep title="React element">
<AnatomyStep title="React node">
The UI you want to render.
</AnatomyStep>
<AnatomyStep title="DOM container">
<AnatomyStep title="DOM node">
The DOM node you want to render your UI into. The container itself isn’t modified, only its children are.
</AnatomyStep>
```js [[1, 2, "<App />"], [2, 2, "container"]]
const container = document.getElementById('root');
render(<App />, container);
```js [[1, 2, "<App />"], [2, 2, "domNode"]]
const domNode = document.getElementById('root');
render(<App />, domNode);
```
</APIAnatomy>
@ -58,9 +118,9 @@ export default function App() {
</Sandpack>
<br />
---
## Rendering multiple roots {/*rendering-multiple-roots*/}
### Rendering multiple roots {/*rendering-multiple-roots*/}
If you use ["sprinkles"](/learn/add-react-to-a-website) of React here and there, call `render` for each top-level piece of UI managed by React.
@ -132,9 +192,9 @@ nav ul li { display: inline-block; margin-right: 20px; }
</Sandpack>
<br />
---
## Updating the rendered tree {/*updating-the-rendered-tree*/}
### Updating the rendered tree {/*updating-the-rendered-tree*/}
You can call `render` more than once on the same DOM node. As long as the component tree structure matches up with what was previously rendered, React will [preserve the state](/learn/preserving-and-resetting-state). Notice how you can type in the input:
@ -168,21 +228,3 @@ export default function App({counter}) {
</Sandpack>
You can destroy the rendered tree with [`unmountComponentAtNode()`](TODO).
<br />
## When not to use it {/*when-not-to-use-it*/}
* If your app uses server rendering and generates HTML on the server, use [`hydrate`](TODO) instead of `render`.
* If your app is fully built with React, you shouldn't need to use `render` more than once. If you want to render something in a different part of the DOM tree (for example, a modal or a tooltip), use [`createPortal`](TODO) instead.
<br />
## Behavior in detail {/*behavior-in-detail*/}
The first time you call `render`, any existing DOM elements inside `container` are replaced. If you call `render` again, React will update the DOM as necessary to reflect the latest JSX. React will decide which parts of the DOM can be reused and which need to be recreated by ["matching it up"](/learn/preserving-and-resetting-state) with the previously rendered tree. Calling `render` repeatedly is similar to calling `setState`--in both cases, React avoids unnecessary DOM updates.
You can pass a callback as the third argument. React will call it after your component is in the DOM.
If you render `<MyComponent />`, and `MyComponent` is a class component, `render` will return the instance of that class. In all other cases, it will return `null`.

Loading…
Cancel
Save