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
- [Updating the rendered tree](#updating-the-rendered-tree)
20
-
21
-
## Reference {/*reference*/}
22
-
23
-
### `render(reactNode, domNode)` {/*render*/}
24
-
25
-
Call `render` to display a React component inside a browser DOM element.
26
-
27
-
```js
28
-
constdomNode=document.getElementById('root');
29
-
render(<App />, domNode);
30
-
```
31
-
32
-
React will display `<App />` in the `domNode`, and take over managing the DOM inside it.
33
-
34
-
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.
35
-
36
-
[See examples below.](#usage)
37
-
38
-
#### Parameters {/*parameters*/}
39
-
40
-
*`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 element constructed with [`createElement()`](/TODO), a string, a number, `null`, or `undefined`.
41
-
42
-
*`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.
43
-
44
-
#### Returns {/*returns*/}
45
-
46
-
`render` usually returns `null`. However, if the `reactNode` you pass is a *class component*, then it will return an instance of that component.
47
-
48
-
#### Caveats {/*caveats*/}
49
-
50
-
* 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.
51
-
52
-
* 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.
53
-
54
-
* 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*/}
100
36
101
-
In apps fully built with React, you will do this once at the top level of your app--to render the "root" component.
37
+
In apps fully built with React, **you will usually only dothis once at startup**--to render the "root" component.
102
38
103
39
<Sandpack>
104
40
@@ -118,11 +54,13 @@ export default function App() {
118
54
119
55
</Sandpack>
120
56
57
+
Usually you shouldn't need to call `render` again or to call it in more places. From this point on, React will be managing the DOM of your application. If you want to update the UI, your components can do this by [using state](/apis/usestate).
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.
63
+
If your page [isn't fully built with React](/learn/add-react-to-a-website), call `render`for each top-level piece ofUI managed by React.
126
64
127
65
<Sandpack>
128
66
@@ -192,16 +130,19 @@ nav ul li { display: inline-block; margin-right: 20px; }
192
130
193
131
</Sandpack>
194
132
133
+
You can destroy the rendered trees with [`unmountComponentAtNode()`](TODO).
134
+
195
135
---
196
136
197
137
### Updating the rendered tree {/*updating-the-rendered-tree*/}
198
138
199
-
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:
139
+
You can call `render` more than once on the same DOMnode. 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, which means that the updates from repeated `render` calls every second inthis example are not destructive:
200
140
201
141
<Sandpack>
202
142
203
143
```js index.js active
204
144
import {render} from 'react-dom';
145
+
import './styles.css';
205
146
import App from './App.js';
206
147
207
148
let i = 0;
@@ -227,4 +168,46 @@ export default function App({counter}) {
227
168
228
169
</Sandpack>
229
170
230
-
You can destroy the rendered tree with [`unmountComponentAtNode()`](TODO).
171
+
It is uncommon to call `render` multiple times. Usually, you'll [update state](/apis/usestate) inside one of the components instead.
Call `render` to display a React component inside a browser DOM element.
180
+
181
+
```js
182
+
const domNode = document.getElementById('root');
183
+
render(<App />, domNode);
184
+
```
185
+
186
+
React will display `<App />` in the `domNode`, and take over managing the DOM inside it.
187
+
188
+
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.
189
+
190
+
[See examples above.](#usage)
191
+
192
+
#### Parameters {/*parameters*/}
193
+
194
+
* `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 element constructed with [`createElement()`](/TODO), a string, a number, `null`, or `undefined`.
195
+
196
+
* `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.
197
+
198
+
* **optional** `callback`: A function. If passed, React will call it after your component is placed into the DOM.
199
+
200
+
201
+
#### Returns {/*returns*/}
202
+
203
+
`render` usually returns `null`. However, if the `reactNode` you pass is a *class component*, then it will return an instance of that component.
204
+
205
+
#### Caveats {/*caveats*/}
206
+
207
+
* 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.
208
+
209
+
* 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.
210
+
211
+
* 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 dothis call for you.) When you want to render a piece ofJSXin 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`.
0 commit comments