Skip to content

Commit 0209e1b

Browse files
authored
[Beta] Reorder API sections (#4328)
* Reorder API pages * Tweaks
1 parent b4de700 commit 0209e1b

5 files changed

Lines changed: 414 additions & 262 deletions

File tree

beta/src/components/MDX/MDXComponents.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44

55
import * as React from 'react';
6+
import cn from 'classnames';
67

78
import {APIAnatomy, AnatomyStep} from './APIAnatomy';
89
import CodeBlock from './CodeBlock';
@@ -26,6 +27,24 @@ import {Challenges, Hint, Solution} from './Challenges';
2627
import {IconNavArrow} from '../Icon/IconNavArrow';
2728
import ButtonLink from 'components/ButtonLink';
2829

30+
function CodeStep({children, step}: {children: any; step: number}) {
31+
return (
32+
<span
33+
data-step={step}
34+
className={cn(
35+
'code-step bg-opacity-10 relative rounded-md p-1 ml-2 pl-3 before:content-[attr(data-step)] before:block before:w-4 before:h-4 before:absolute before:top-1 before:-left-2 before:rounded-full before:text-white before:text-center before:text-xs before:leading-4',
36+
{
37+
'bg-blue-40 before:bg-blue-40': step === 1,
38+
'bg-yellow-40 before:bg-yellow-40': step === 2,
39+
'bg-green-40 before:bg-green-40': step === 3,
40+
'bg-purple-40 before:bg-purple-40': step === 4,
41+
}
42+
)}>
43+
{children}
44+
</span>
45+
);
46+
}
47+
2948
const P = (p: JSX.IntrinsicElements['p']) => (
3049
<p className="whitespace-pre-wrap my-4" {...p} />
3150
);
@@ -373,4 +392,5 @@ export const MDXComponents = {
373392
Challenges,
374393
Hint,
375394
Solution,
395+
CodeStep,
376396
};

beta/src/pages/apis/index.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,16 @@ If you use React on the web, you'll also need the same version of [ReactDOM](/ap
3434

3535
## Exports {/*exports*/}
3636

37-
<YouWillLearnCard title="useState" path="/reference/usestate">
37+
<YouWillLearnCard title="useState" path="/apis/usestate">
38+
39+
Declares a state variable.
40+
41+
```js
42+
function MyComponent() {
43+
const [age, setAge] = useState(42);
44+
// ...
45+
```
3846
39-
A React Hook that lets a component "remember" some information (called state).
4047
4148
</YouWillLearnCard>
4249

beta/src/pages/apis/reactdom.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,13 @@ ReactDOM supports all popular browsers, including Internet Explorer 9 and above.
3838

3939
## Exports {/*exports*/}
4040

41-
<YouWillLearnCard title="render" path="/reference/render">
41+
<YouWillLearnCard title="render" path="/apis/render">
4242

43-
Renders a piece of JSX ("React element") into a browser DOM container node.
43+
Displays a React component inside a browser DOM node.
44+
45+
```js
46+
render(<App />, document.getElementById('root'));
47+
```
4448

4549
</YouWillLearnCard>
4650

beta/src/pages/apis/render.md

Lines changed: 65 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,40 @@
11
---
2-
title: render()
2+
title: render
33
---
44

55
<Intro>
66

77
`render` renders a piece of [JSX](/learn/writing-markup-with-jsx) ("React node") into a browser DOM node.
88

9-
</Intro>
9+
```js
10+
render(reactNode, domNode, callback?)
11+
```
1012
11-
## On this page {/*on-this-page*/}
13+
</Intro>
1214
13-
- [Reference](#reference)
14-
- [`render(reactNode, domNode)`](#render)
15-
- [`render(reactNode, domNode, callback)`](#render-callback)
1615
- [Usage](#usage)
1716
- [Rendering the root component](#rendering-the-root-component)
1817
- [Rendering multiple roots](#rendering-multiple-roots)
1918
- [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-
const domNode = 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`.
55-
56-
---
57-
58-
### `render(reactNode, domNode, callback)` {/*render-callback*/}
59-
60-
Same as [`render(reactNode, domNode)`](#render), but with a `callback` that notifies you when your component has been placed into the DOM.
61-
62-
#### Parameters {/*render-callback-parameters*/}
63-
64-
* `reactNode`: [Same as above.](#parameters)
65-
* `domNode`: [Same as above.](#parameters)
66-
* `callback`: A function. If passed, React will call it after your component is placed into the DOM.
67-
68-
#### Returns {/*render-callback-returns*/}
69-
70-
[Same as above.](#returns)
19+
- [Reference](#reference)
20+
- [`render(reactNode, domNode, callback?)`](#render)
7121
7222
---
7323
7424
## Usage {/*usage*/}
7525
76-
### Rendering the root component {/*rendering-the-root-component*/}
77-
78-
To call `render`, you need a piece of JSX and a DOM container:
79-
80-
<APIAnatomy>
81-
82-
<AnatomyStep title="React node">
83-
84-
The UI you want to render.
26+
Call `render` to display a <CodeStep step={1}>React component</CodeStep> inside a <CodeStep step={2}>browser DOM node</CodeStep>.
8527
86-
</AnatomyStep>
87-
88-
<AnatomyStep title="DOM node">
89-
90-
The DOM node you want to render your UI into. The container itself isn’t modified, only its children are.
91-
92-
</AnatomyStep>
28+
```js [[1, 4, "<App />"], [2, 4, "document.getElementById('root')"]]
29+
import {render} from 'react-dom';
30+
import App from './App.js';
9331

94-
```js [[1, 2, "<App />"], [2, 2, "domNode"]]
95-
const domNode = document.getElementById('root');
96-
render(<App />, domNode);
97-
```
32+
render(<App />, document.getElementById('root'));
33+
````
9834

99-
</APIAnatomy>
35+
### Rendering the root component {/*rendering-the-root-component*/}
10036

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 do this once at startup**--to render the "root" component.
10238

10339
<Sandpack>
10440

@@ -118,11 +54,13 @@ export default function App() {
11854

11955
</Sandpack>
12056

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).
58+
12159
---
12260
12361
### Rendering multiple roots {/*rendering-multiple-roots*/}
12462
125-
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 of UI managed by React.
12664

12765
<Sandpack>
12866

@@ -192,16 +130,19 @@ nav ul li { display: inline-block; margin-right: 20px; }
192130

193131
</Sandpack>
194132

133+
You can destroy the rendered trees with [`unmountComponentAtNode()`](TODO).
134+
195135
---
196136

197137
### Updating the rendered tree {/*updating-the-rendered-tree*/}
198138

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 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, which means that the updates from repeated `render` calls every second in this example are not destructive:
200140

201141
<Sandpack>
202142

203143
```js index.js active
204144
import {render} from 'react-dom';
145+
import './styles.css';
205146
import App from './App.js';
206147
207148
let i = 0;
@@ -227,4 +168,46 @@ export default function App({counter}) {
227168

228169
</Sandpack>
229170

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.
172+
173+
---
174+
175+
## Reference {/*reference*/}
176+
177+
### `render(reactNode, domNode, callback?)` {/*render*/}
178+
179+
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 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`.
212+
213+
---

0 commit comments

Comments
 (0)