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
Copy file name to clipboardExpand all lines: src/content/learn/render-and-commit.md
+48-46
Original file line number
Diff line number
Diff line change
@@ -1,44 +1,46 @@
1
1
---
2
-
title: Render and Commit
2
+
title: Render ແລະ Commit
3
3
---
4
4
5
5
<Intro>
6
6
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.
*The steps involved in displaying a component on screen
16
-
*Why rendering does not always produce a DOM update
13
+
*ການ render ໃນ React ແມ່ນຫຍັງ
14
+
*ເມື່ອໃດ ແລະ ຍ້ອນຫຍັງ React ຈຶ່ງ render component
15
+
*ຂັ້ນຕອນທີ່ກ່ຽວຂ້ອງໃນການສະແດງ component ເທິງໜ້າຈໍ
16
+
*ເປັນຫຍັງການ render ຈຶ່ງບໍ່ສ້າງການອັບເດດ DOM ທຸກຄັ້ງ
17
17
18
18
</YouWillLearn>
19
19
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:
<Illustrationcaption="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
30
<Illustrationcaption="Render"alt="The Card Chef gives React a fresh Card component."src="/images/docs/illustrations/i_render-and-commit2.png" />
29
31
<Illustrationcaption="Commit"alt="React delivers the Card to the user at their table."src="/images/docs/illustrations/i_render-and-commit3.png" />
30
32
</IllustrationBlock>
31
33
32
-
## Step 1: Trigger a render {/*step-1-trigger-a-render*/}
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:
### Re-renders when state updates {/*re-renders-when-state-updates*/}
70
+
### ການ render ໃໝ່ເມື່ອ state ມີການອັບເດດ {/*re-renders-when-state-updates*/}
69
71
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.)
<Illustrationcaption="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. They patron expresses they want a pink card, not a black one!"src="/images/docs/illustrations/i_rerender1.png" />
74
76
<Illustrationcaption="...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
77
<Illustrationcaption="...render!"alt="The Card Chef gives React the pink Card."src="/images/docs/illustrations/i_rerender3.png" />
76
78
</IllustrationBlock>
77
79
78
-
## Step 2: React renders your components {/*step-2-react-renders-your-components*/}
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.
***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.
***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.)
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.
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!**
***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.
155
+
***ສຳລັບການ render ເລີ່ມຕົ້ນ,** React ຈະໃຊ້ [`appendChild()`](https://developer.mozilla.org/docs/Web/API/Node/appendChild)DOM API ເພື່ອວາງ DOM node ທັງໝົດ ມັນຖືກສ້າງຂຶ້ນເທິງໜ້າຈໍ.
**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:
@@ -193,21 +195,21 @@ export default function App() {
193
195
194
196
</Sandpack>
195
197
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`!
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.
0 commit comments