Skip to content

Commit 5023615

Browse files
committed
docs: enhance clarity and add analogies in render and commit guide for better understanding of React's rendering cycle
1 parent 84a5696 commit 5023615

File tree

1 file changed

+72
-107
lines changed

1 file changed

+72
-107
lines changed
Lines changed: 72 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,46 @@
11
---
2-
title: Render and Commit
3-
---
4-
5-
<Intro>
62

7-
Before your components are displayed on screen, they must be rendered by React. Understanding the steps in this process will help you think about how your code executes and explain its behavior.
3+
## title: Render and Commit
84

9-
</Intro>
5+
Before your components appear on screen, they must first be rendered by React. Understanding this process helps you predict your app’s behavior and debug more effectively.
106

11-
<YouWillLearn>
7+
This guide covers:
128

13-
* What rendering means in React
14-
* When and why React renders a component
15-
* The steps involved in displaying a component on screen
16-
* Why rendering does not always produce a DOM update
9+
- What rendering means in React
10+
- When and why components render
11+
- How rendering leads to updates on the screen
12+
- Why not every render results in a DOM update
1713

18-
</YouWillLearn>
14+
---
1915

20-
Imagine that your components are cooks in the kitchen, assembling tasty dishes from ingredients. In this scenario, React is the waiter who puts in requests from customers and brings them their orders. This process of requesting and serving UI has three steps:
16+
Imagine your components as chefs preparing meals in a kitchen. React is the waiter who takes the customer’s order and ensures the correct dish reaches the table. The full process of serving UI includes three steps:
2117

22-
1. **Triggering** a render (delivering the guest's order to the kitchen)
23-
2. **Rendering** the component (preparing the order in the kitchen)
24-
3. **Committing** to the DOM (placing the order on the table)
18+
1. **Triggering** a render (the waiter takes the order to the kitchen)
19+
2. **Rendering** the component (the chef prepares the dish)
20+
3. **Committing** to the DOM (the waiter places the dish on the table)
2521

26-
<IllustrationBlock sequential>
27-
<Illustration caption="Trigger" alt="React as a server in a restaurant, fetching orders from the users and delivering them to the Component Kitchen." src="/images/docs/illustrations/i_render-and-commit1.png" />
28-
<Illustration caption="Render" alt="The Card Chef gives React a fresh Card component." src="/images/docs/illustrations/i_render-and-commit2.png" />
29-
<Illustration caption="Commit" alt="React delivers the Card to the user at their table." src="/images/docs/illustrations/i_render-and-commit3.png" />
30-
</IllustrationBlock>
22+
---
3123

3224
## Step 1: Trigger a render {/*step-1-trigger-a-render*/}
3325

34-
There are two reasons for a component to render:
26+
React renders a component for two main reasons:
3527

36-
1. It's the component's **initial render.**
37-
2. The component's (or one of its ancestors') **state has been updated.**
28+
1. It's the component's **initial render**
29+
2. The component or one of its ancestors **has updated its state**
3830

3931
### Initial render {/*initial-render*/}
4032

41-
When your app starts, you need to trigger the initial render. Frameworks and sandboxes sometimes hide this code, but it's done by calling [`createRoot`](/reference/react-dom/client/createRoot) with the target DOM node, and then calling its `render` method with your component:
42-
43-
<Sandpack>
33+
When your app starts, you trigger the first render by calling [`createRoot`](/reference/react-dom/client/createRoot) with a DOM node and rendering your component:
4434

45-
```js src/index.js active
35+
```js
4636
import Image from './Image.js';
4737
import { createRoot } from 'react-dom/client';
4838

49-
const root = createRoot(document.getElementById('root'))
39+
const root = createRoot(document.getElementById('root'));
5040
root.render(<Image />);
51-
```
52-
53-
```js src/Image.js
41+
js
42+
Copy
43+
Edit
5444
export default function Image() {
5545
return (
5646
<img
@@ -59,36 +49,27 @@ export default function Image() {
5949
/>
6050
);
6151
}
62-
```
52+
🔍 Try commenting out root.render() to see that the component won’t appear at all.
6353

64-
</Sandpack>
54+
Re-renders when state updates {/re-renders-when-state-updates/}
55+
After the initial render, you can trigger more renders by updating component state using setState. When state changes, React automatically queues a re-render.
6556

66-
Try commenting out the `root.render()` call and see the component disappear!
57+
Think of it like a diner adding dessert to their order—the waiter (React) needs to bring something new based on the customer’s current appetite (component state).
6758

68-
### Re-renders when state updates {/*re-renders-when-state-updates*/}
59+
Step 2: React renders your components {/step-2-react-renders-your-components/}
60+
Once triggered, React calls your component functions to figure out what to display.
6961

70-
Once the component has been initially rendered, you can trigger further renders by updating its state with the [`set` function.](/reference/react/useState#setstate) Updating your component's state automatically queues a render. (You can imagine these as a restaurant guest ordering tea, dessert, and all sorts of things after putting in their first order, depending on the state of their thirst or hunger.)
62+
On the initial render, React calls the root component.
7163

72-
<IllustrationBlock sequential>
73-
<Illustration caption="State update..." alt="React as a server in a restaurant, serving a Card UI to the user, represented as a patron with a cursor for their head. The patron expresses they want a pink card, not a black one!" src="/images/docs/illustrations/i_rerender1.png" />
74-
<Illustration caption="...triggers..." alt="React returns to the Component Kitchen and tells the Card Chef they need a pink Card." src="/images/docs/illustrations/i_rerender2.png" />
75-
<Illustration caption="...render!" alt="The Card Chef gives React the pink Card." src="/images/docs/illustrations/i_rerender3.png" />
76-
</IllustrationBlock>
64+
On subsequent renders, React re-calls the component whose state changed.
7765

78-
## Step 2: React renders your components {/*step-2-react-renders-your-components*/}
66+
This is a recursive process: if a component returns other components, React will render those next, continuing until all nested components are processed and it knows what to display.
7967

80-
After you trigger a render, React calls your components to figure out what to display on screen. **"Rendering" is React calling your components.**
68+
Here’s an example:
8169

82-
* **On initial render,** React will call the root component.
83-
* **For subsequent renders,** React will call the function component whose state update triggered the render.
84-
85-
This process is recursive: if the updated component returns some other component, React will render _that_ component next, and if that component also returns something, it will render _that_ component next, and so on. The process will continue until there are no more nested components and React knows exactly what should be displayed on screen.
86-
87-
In the following example, React will call `Gallery()` and `Image()` several times:
88-
89-
<Sandpack>
90-
91-
```js src/Gallery.js active
70+
js
71+
Copy
72+
Edit
9273
export default function Gallery() {
9374
return (
9475
<section>
@@ -108,56 +89,45 @@ function Image() {
10889
/>
10990
);
11091
}
111-
```
112-
113-
```js src/index.js
92+
js
93+
Copy
94+
Edit
11495
import Gallery from './Gallery.js';
11596
import { createRoot } from 'react-dom/client';
11697

117-
const root = createRoot(document.getElementById('root'))
98+
const root = createRoot(document.getElementById('root'));
11899
root.render(<Gallery />);
119-
```
120-
121-
```css
122-
img { margin: 0 10px 10px 0; }
123-
```
124-
125-
</Sandpack>
126-
127-
* **During the initial render,** React will [create the DOM nodes](https://developer.mozilla.org/docs/Web/API/Document/createElement) for `<section>`, `<h1>`, and three `<img>` tags.
128-
* **During a re-render,** React will calculate which of their properties, if any, have changed since the previous render. It won't do anything with that information until the next step, the commit phase.
129-
130-
<Pitfall>
131-
132-
Rendering must always be a [pure calculation](/learn/keeping-components-pure):
133-
134-
* **Same inputs, same output.** Given the same inputs, a component should always return the same JSX. (When someone orders a salad with tomatoes, they should not receive a salad with onions!)
135-
* **It minds its own business.** It should not change any objects or variables that existed before rendering. (One order should not change anyone else's order.)
136-
137-
Otherwise, you can encounter confusing bugs and unpredictable behavior as your codebase grows in complexity. When developing in "Strict Mode", React calls each component's function twice, which can help surface mistakes caused by impure functions.
138-
139-
</Pitfall>
100+
css
101+
Copy
102+
Edit
103+
img {
104+
margin: 0 10px 10px 0;
105+
}
106+
During initial render, React creates DOM nodes for <section>, <h1>, and three <img> tags.
140107

141-
<DeepDive>
108+
During a re-render, it compares the output to the previous render to decide if the DOM needs to change.
142109

143-
#### Optimizing performance {/*optimizing-performance*/}
110+
Pure rendering functions
111+
React rendering should be a pure calculation:
144112

145-
The default behavior of rendering all components nested within the updated component is not optimal for performance if the updated component is very high in the tree. If you run into a performance issue, there are several opt-in ways to solve it described in the [Performance](https://reactjs.org/docs/optimizing-performance.html) section. **Don't optimize prematurely!**
113+
Same input → same output. A component should return the same JSX given the same props and state.
146114

147-
</DeepDive>
115+
No side effects. Components shouldn’t modify external variables or state during render.
148116

149-
## Step 3: React commits changes to the DOM {/*step-3-react-commits-changes-to-the-dom*/}
117+
🧪 In Strict Mode, React may call your component functions twice (in dev mode only) to help you catch impure code early.
150118

151-
After rendering (calling) your components, React will modify the DOM.
119+
Step 3: React commits changes to the DOM {/step-3-react-commits-changes-to-the-dom/}
120+
Once React finishes rendering, it moves on to updating the DOM.
152121

153-
* **For the initial render,** React will use the [`appendChild()`](https://developer.mozilla.org/docs/Web/API/Node/appendChild) DOM API to put all the DOM nodes it has created on screen.
154-
* **For re-renders,** React will apply the minimal necessary operations (calculated while rendering!) to make the DOM match the latest rendering output.
122+
During the first render, it uses appendChild() to insert the new DOM nodes.
155123

156-
**React only changes the DOM nodes if there's a difference between renders.** For example, here is a component that re-renders with different props passed from its parent every second. Notice how you can add some text into the `<input>`, updating its `value`, but the text doesn't disappear when the component re-renders:
124+
On subsequent renders, React calculates the minimal set of changes and applies only what's necessary.
157125
158-
<Sandpack>
126+
For example:
159127
160-
```js src/Clock.js active
128+
js
129+
Copy
130+
Edit
161131
export default function Clock({ time }) {
162132
return (
163133
<>
@@ -166,9 +136,9 @@ export default function Clock({ time }) {
166136
</>
167137
);
168138
}
169-
```
170-
171-
```js src/App.js hidden
139+
js
140+
Copy
141+
Edit
172142
import { useState, useEffect } from 'react';
173143
import Clock from './Clock.js';
174144
@@ -189,25 +159,20 @@ export default function App() {
189159
<Clock time={time.toLocaleTimeString()} />
190160
);
191161
}
192-
```
162+
You can type into the <input> and your text won’t disappear when the clock re-renders every second. That’s because React sees the input node hasn’t changed, so it doesn’t touch it.
193163
194-
</Sandpack>
164+
Epilogue: Browser paint {/epilogue-browser-paint/}
165+
After React commits changes to the DOM, the browser repaints the screen.
195166
196-
This works because during this last step, React only updates the content of `<h1>` with the new `time`. It sees that the `<input>` appears in the JSX in the same place as last time, so React doesn't touch the `<input>`—or its `value`!
197-
## Epilogue: Browser paint {/*epilogue-browser-paint*/}
167+
Although this process is sometimes called “browser rendering,” we use the term painting to avoid confusion with React rendering.
198168
199-
After rendering is done and React updated the DOM, the browser will repaint the screen. Although this process is known as "browser rendering", we'll refer to it as "painting" to avoid confusion throughout the docs.
169+
Summary
170+
UI updates happen in three steps: Trigger → Render → Commit
200171
201-
<Illustration alt="A browser painting 'still life with card element'." src="/images/docs/illustrations/i_browser-paint.png" />
172+
React rendering is a pure function of props and state
202173
203-
<Recap>
174+
React commits only the necessary DOM changes
204175
205-
* Any screen update in a React app happens in three steps:
206-
1. Trigger
207-
2. Render
208-
3. Commit
209-
* You can use Strict Mode to find mistakes in your components
210-
* React does not touch the DOM if the rendering result is the same as last time
176+
Use Strict Mode to catch mistakes in render logic
211177
212-
</Recap>
213178

0 commit comments

Comments
 (0)