Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 99f180e

Browse files
zzwghzhaozhiweidaochouwangu
authoredMar 1, 2023
[Beta] docs(cn): translate state-as-a-snapshot into Chinese (#643)
* docs(cn): state-as-a-snapshot * docs(cn): state-as-a-snapshot * fix: syntax error * docs(cn): resolve code reviews * docs(cn): resolve code reviews * Update beta/src/content/learn/state-as-a-snapshot.md Co-authored-by: TimLi <daochouwangu@gmail.com> * Update beta/src/content/learn/state-as-a-snapshot.md Co-authored-by: TimLi <daochouwangu@gmail.com> * Update beta/src/content/learn/state-as-a-snapshot.md Co-authored-by: TimLi <daochouwangu@gmail.com> --------- Co-authored-by: zhaozhiwei <zhaozhiwei@jwzg.com> Co-authored-by: TimLi <daochouwangu@gmail.com>
1 parent a92b92a commit 99f180e

File tree

1 file changed

+77
-76
lines changed

1 file changed

+77
-76
lines changed
 

‎beta/src/content/learn/state-as-a-snapshot.md

+77-76
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
11
---
2-
title: State as a Snapshot
2+
title: state 如同一张快照
3+
translators:
4+
- zzw
5+
- Neo42
36
---
47

58
<Intro>
69

7-
State variables might look like regular JavaScript variables that you can read and write to. However, state behaves more like a snapshot. Setting it does not change the state variable you already have, but instead triggers a re-render.
10+
也许 state 变量看起来和一般的可读写的 JavaScript 变量类似。但 state 在其表现出的特性上更像是一张快照。设置它不会更改你已有的 state 变量,但会触发重新渲染。
811

912
</Intro>
1013

1114
<YouWillLearn>
1215

13-
* How setting state triggers re-renders
14-
* When and how state updates
15-
* Why state does not update immediately after you set it
16-
* How event handlers access a "snapshot" of the state
16+
* 设置 state 如何导致重新渲染
17+
* state 在何时以何种方式更新
18+
* 为什么 state 不在设置后立即更新
19+
* 事件处理函数如何获取 state 的一张“快照”
1720

1821
</YouWillLearn>
1922

20-
## Setting state triggers renders {/*setting-state-triggers-renders*/}
23+
## 设置 state 会触发渲染 {/*setting-state-triggers-renders*/}
2124

22-
You might think of your user interface as changing directly in response to the user event like a click. In React, it works a little differently from this mental model. On the previous page, you saw that [setting state requests a re-render](/learn/render-and-commit#step-1-trigger-a-render) from React. This means that for an interface to react to the event, you need to *update the state*.
25+
你可能会认为你的用户界面会直接对点击之类的用户输入做出响应并发生变化。在 React 中,它的工作方式与这种思维模型略有不同。在上一页中,你看到了来自 React[设置 state 请求重新渲染](/learn/render-and-commit#step-1-trigger-a-render)。这意味着要使界面对输入做出反应,你需要设置其 state
2326

24-
In this example, when you press "send", `setIsSent(true)` tells React to re-render the UI:
27+
在这个例子中,当你按下 “send” 时,`setIsSent(true)` 会通知 React 重新渲染 UI:
2528

2629
<Sandpack>
2730

@@ -61,43 +64,43 @@ label, textarea { margin-bottom: 10px; display: block; }
6164

6265
</Sandpack>
6366

64-
Here's what happens when you click the button:
67+
当你单击按钮时会发生以下情况:
6568

66-
1. The `onSubmit` event handler executes.
67-
2. `setIsSent(true)` sets `isSent` to `true` and queues a new render.
68-
3. React re-renders the component according to the new `isSent` value.
69+
1. 执行 `onSubmit` 事件处理函数。
70+
2. `setIsSent(true)` `isSent` 设置为 `true` 并排列一个新的渲染。
71+
3. React 根据新的 `isSent` 值重新渲染组件。
6972

70-
Let's take a closer look at the relationship between state and rendering.
73+
让我们仔细看看 state 和渲染之间的关系。
7174

72-
## Rendering takes a snapshot in time {/*rendering-takes-a-snapshot-in-time*/}
75+
## 渲染会及时生成一张快照 {/*rendering-takes-a-snapshot-in-time*/}
7376

74-
["Rendering"](/learn/render-and-commit#step-2-react-renders-your-components) means that React is calling your component, which is a function. The JSX you return from that function is like a snapshot of the UI in time. Its props, event handlers, and local variables were all calculated **using its state at the time of the render.**
77+
["正在渲染"](/learn/render-and-commit#step-2-react-renders-your-components) 就意味着 React 正在调用你的组件——一个函数。你从该函数返回的 JSX 就像是 UI 的一张及时的快照。它的 props、事件处理函数和内部变量都是 **根据当前渲染时的 state** 被计算出来的。
7578

76-
Unlike a photograph or a movie frame, the UI "snapshot" you return is interactive. It includes logic like event handlers that specify what happens in response to inputs. React then updates the screen to match this snapshot and connects the event handlers. As a result, pressing a button will trigger the click handler from your JSX.
79+
与照片或电影画面不同,你返回的 UI “快照”是可交互的。它其中包括类似事件处理函数的逻辑,这些逻辑用于指定如何对输入作出响应。React 随后会更新屏幕来匹配这张快照,并绑定事件处理函数。因此,按下按钮就会触发你 JSX 中的点击事件处理函数。
7780

78-
When React re-renders a component:
81+
React 重新渲染一个组件时:
7982

80-
1. React calls your function again.
81-
2. Your function returns a new JSX snapshot.
82-
3. React then updates the screen to match the snapshot you've returned.
83+
1. React 会再次调用你的函数
84+
2. 你的函数会返回新的 JSX 快照
85+
3. React 会更新界面来匹配你返回的快照
8386

84-
<IllustrationBlock sequential>
85-
<Illustration caption="React executing the function" src="/images/docs/illustrations/i_render1.png" />
86-
<Illustration caption="Calculating the snapshot" src="/images/docs/illustrations/i_render2.png" />
87-
<Illustration caption="Updating the DOM tree" src="/images/docs/illustrations/i_render3.png" />
87+
<IllustrationBlock title="重新渲染" sequential>
88+
<Illustration caption="React 执行函数" src="/images/docs/illustrations/i_render1.png" />
89+
<Illustration caption="计算快照" src="/images/docs/illustrations/i_render2.png" />
90+
<Illustration caption="更新 DOM " src="/images/docs/illustrations/i_render3.png" />
8891
</IllustrationBlock>
8992

90-
As a component's memory, state is not like a regular variable that disappears after your function returns. State actually "lives" in React itself--as if on a shelf!--outside of your function. When React calls your component, it gives you a snapshot of the state for that particular render. Your component returns a snapshot of the UI with a fresh set of props and event handlers in its JSX, all calculated **using the state values from that render!**
93+
作为一个组件的记忆,state 不同于在你的函数返回之后就会消失的普通变量。state 实际上“活”在 React 本身中——就像被摆在一个架子上!——位于你的函数之外。当 React 调用你的组件时,它会为特定的那一次渲染提供一张 state 快照。你的组件会在其 JSX 中返回一张包含一整套新的 props 和事件处理函数的 UI 快照 ,其中所有的值都是 **根据那一次渲染中 state 的值** 被计算出来的!
9194

9295
<IllustrationBlock sequential>
93-
<Illustration caption="You tell React to update the state" src="/images/docs/illustrations/i_state-snapshot1.png" />
94-
<Illustration caption="React updates the state value" src="/images/docs/illustrations/i_state-snapshot2.png" />
95-
<Illustration caption="React passes a snapshot of the state value into the component" src="/images/docs/illustrations/i_state-snapshot3.png" />
96+
<Illustration caption="React 收到 setUpdate 通知" src="/images/docs/illustrations/i_state-snapshot1.png" />
97+
<Illustration caption="React 更新 state 的值" src="/images/docs/illustrations/i_state-snapshot2.png" />
98+
<Illustration caption="React 向组件内传入一张 state 的快照" src="/images/docs/illustrations/i_state-snapshot3.png" />
9699
</IllustrationBlock>
97100

98-
Here's a little experiment to show you how this works. In this example, you might expect that clicking the "+3" button would increment the counter three times because it calls `setNumber(number + 1)` three times.
101+
这里有个向你展示其运行原理的小例子。在这个例子中,你可能会以为点击“+3”按钮会调用 `setNumber(number + 1)` 三次从而使计数器递增三次。
99102

100-
See what happens when you click the "+3" button:
103+
看看你点击“+3”按钮时会发生什么:
101104

102105
<Sandpack>
103106

@@ -127,9 +130,9 @@ h1 { display: inline-block; margin: 10px; width: 30px; text-align: center; }
127130

128131
</Sandpack>
129132

130-
Notice that `number` only increments once per click!
133+
请注意,每次点击只会让 `number` 递增一次!
131134

132-
**Setting state only changes it for the *next* render.** During the first render, `number` was `0`. This is why, in *that render's* `onClick` handler, the value of `number` is still `0` even after `setNumber(number + 1)` was called:
135+
**设置 state 只会为 *下一次* 渲染变更 state 的值。**在第一次渲染期间,`number` `0`。这也就解释了为什么在 *那次渲染中的* `onClick` 处理函数中,即便在调用了 `setNumber(number + 1)` 之后,`number` 的值也仍然是 `0`
133136

134137
```js
135138
<button onClick={() => {
@@ -139,18 +142,18 @@ Notice that `number` only increments once per click!
139142
}}>+3</button>
140143
```
141144

142-
Here is what this button's click handler tells React to do:
145+
以下是这个按钮的点击事件处理函数通知 React 要做的事情:
143146

144-
1. `setNumber(number + 1)`: `number` is `0` so `setNumber(0 + 1)`.
145-
- React prepares to change `number` to `1` on the next render.
146-
2. `setNumber(number + 1)`: `number` is `0` so `setNumber(0 + 1)`.
147-
- React prepares to change `number` to `1` on the next render.
148-
3. `setNumber(number + 1)`: `number` is `0` so `setNumber(0 + 1)`.
149-
- React prepares to change `number` to `1` on the next render.
147+
1. `setNumber(number + 1)``number` `0` 所以 `setNumber(0 + 1)`
148+
- React 准备在下一次渲染时将 `number` 更改为 `1`
149+
2. `setNumber(number + 1)``number` `0` 所以 `setNumber(0 + 1)`
150+
- React 准备在下一次渲染时将 `number` 更改为 `1`
151+
3. `setNumber(number + 1)``number` `0` 所以 `setNumber(0 + 1)`
152+
- React 准备在下一次渲染时将 `number` 更改为 `1`
150153

151-
Even though you called `setNumber(number + 1)` three times, in *this render's* event handler `number` is always `0`, so you set the state to `1` three times. This is why, after your event handler finishes, React re-renders the component with `number` equal to `1` rather than `3`.
154+
尽管你调用了三次 `setNumber(number + 1)`,但在 *这次渲染的* 事件处理函数中 `number` 会一直是 `0`,所以你会三次将 state 设置成 `1`。这就是为什么在你的事件处理函数执行完以后,React 重新渲染的组件中的 `number` 等于 `1` 而不是 `3`
152155

153-
You can also visualize this by mentally substituting state variables with their values in your code. Since the `number` state variable is `0` for *this render*, its event handler looks like this:
156+
你还可以通过在心里把 state 变量替换成它们在你代码中的值来想象这个过程。由于 *这次渲染* 中的 state 变量 `number` `0`,其事件处理函数看起来会像这样:
154157

155158
```js
156159
<button onClick={() => {
@@ -159,8 +162,7 @@ You can also visualize this by mentally substituting state variables with their
159162
setNumber(0 + 1);
160163
}}>+3</button>
161164
```
162-
163-
For the next render, `number` is `1`, so *that render's* click handler looks like this:
165+
对于下一次渲染来说,`number``1`,因此 *那次渲染中的* 点击事件处理函数看起来会像这样:
164166

165167
```js
166168
<button onClick={() => {
@@ -169,12 +171,11 @@ For the next render, `number` is `1`, so *that render's* click handler looks lik
169171
setNumber(1 + 1);
170172
}}>+3</button>
171173
```
174+
这就是为什么再次点击按钮会将计数器设置为 `2`,下次点击时会设为 `3`,依此类推。
172175

173-
This is why clicking the button again will set the counter to `2`, then to `3` on the next click, and so on.
174-
175-
## State over time {/*state-over-time*/}
176+
## 随时间变化的 state {/*state-over-time*/}
176177

177-
Well, that was fun. Try to guess what clicking this button will alert:
178+
好的,刚才那些很有意思。试着猜猜点击这个按钮会发出什么警告:
178179

179180
<Sandpack>
180181

@@ -203,14 +204,14 @@ h1 { display: inline-block; margin: 10px; width: 30px; text-align: center; }
203204

204205
</Sandpack>
205206

206-
If you use the substitution method from before, you can guess that the alert shows "0":
207+
如果你使用之前替换的方法,你就能猜到这个提示框将会显示 "0"
207208

208209
```js
209210
setNumber(0 + 5);
210211
alert(0);
211212
```
212213

213-
But what if you put a timer on the alert, so it only fires _after_ the component re-rendered? Would it say "0" or "5"? Have a guess!
214+
但如果你在这个提示框上加上一个定时器, 使得它在组件重新渲染 _之后_ 才触发,又会怎样呢?是会显示 “0” 还是 “5” ?猜一猜!
214215

215216
<Sandpack>
216217

@@ -241,7 +242,7 @@ h1 { display: inline-block; margin: 10px; width: 30px; text-align: center; }
241242

242243
</Sandpack>
243244

244-
Surprised? If you use the substitution method, you can see the "snapshot" of the state passed to the alert.
245+
惊讶吗?你如果使用替代法,就能看到被传入提示框的 state “快照”。
245246

246247
```js
247248
setNumber(0 + 5);
@@ -250,16 +251,16 @@ setTimeout(() => {
250251
}, 3000);
251252
```
252253

253-
The state stored in React may have changed by the time the alert runs, but it was scheduled using a snapshot of the state at the time the user interacted with it!
254+
到提示框运行时,React 中存储的 state 可能已经发生了更改,但是这次更改是根据用户与其交互时的 state 快照来安排的!
254255

255-
**A state variable's value never changes within a render,** even if its event handler's code is asynchronous. Inside *that render's* `onClick`, the value of `number` continues to be `0` even after `setNumber(number + 5)` was called. Its value was "fixed" when React "took the snapshot" of the UI by calling your component.
256+
**一个 state 变量的值永远不会在一次渲染的内部发生变化,** 即使其事件处理函数的代码是异步的。在 *那次渲染的* `onClick` 内部,`number` 的值即使在调用 `setNumber(number + 5)` 之后也还是 `0`。它的值在 React 通过调用你的组件“获取 UI 的快照”时就被“固定”了。
256257

257-
Here is an example of how that makes your event handlers less prone to timing mistakes. Below is a form that sends a message with a five-second delay. Imagine this scenario:
258+
这里有个示例能够说明上述特性会使你的事件处理函数更不容易出现计时错误。下面是一个会在五秒延迟之后发送一条消息的表单。想象以下场景:
258259

259-
1. You press the "Send" button, sending "Hello" to Alice.
260-
2. Before the five-second delay ends, you change the value of the "To" field to "Bob".
260+
1. 你按下“发送”按钮,向 Alice 发送“你好”。
261+
2. 在五秒延迟结束之前,将“To”字段的值更改为“Bob”。
261262

262-
What do you expect the `alert` to display? Would it display, "You said Hello to Alice"? Or would it display, "You said Hello to Bob"? Make a guess based on what you know, and then try it:
263+
你觉得 `alert` 会显示什么?它是会显示“你向 Alice 说了你好“还是会显示“你向 Tom 说了你好”?根据你已经学到的知识猜一猜,然后动手试一试:
263264

264265
<Sandpack>
265266

@@ -305,29 +306,29 @@ label, textarea { margin-bottom: 10px; display: block; }
305306

306307
</Sandpack>
307308

308-
**React keeps the state values "fixed" within one render's event handlers.** You don't need to worry whether the state has changed while the code is running.
309+
**React 会使 state 的值始终”固定“在一次渲染的各个事件处理函数内部。** 你无需担心代码运行时 state 是否发生了变化。
309310

310-
But what if you wanted to read the latest state before a re-render? You'll want to use a [state updater function](/learn/queueing-a-series-of-state-updates), covered on the next page!
311+
但是,万一你想在重新渲染之前读取最新的 state 怎么办?你应该使用 [状态更新函数](/learn/queueing-a-series-of-state-updates),下一页将会介绍!
311312

312313
<Recap>
313314

314-
* Setting state requests a new render.
315-
* React stores state outside of your component, as if on a shelf.
316-
* When you call `useState`, React gives you a snapshot of the state *for that render*.
317-
* Variables and event handlers don't "survive" re-renders. Every render has its own event handlers.
318-
* Every render (and functions inside it) will always "see" the snapshot of the state that React gave to *that* render.
319-
* You can mentally substitute state in event handlers, similarly to how you think about the rendered JSX.
320-
* Event handlers created in the past have the state values from the render in which they were created.
315+
* 设置 state 请求一次新的渲染。
316+
* React state 存储在组件之外,就像在架子上一样。
317+
* 当你调用 `useState` 时,React 会为你提供*该次渲染* 的一张 state 快照。
318+
* 变量和事件处理函数不会在重渲染中“存活”。每个渲染都有自己的事件处理函数。
319+
* 每个渲染(以及其中的函数)始终“看到”的是 React 提供给*这个* 渲染的 state 快照。
320+
* 你可以在心中替换事件处理函数中的 state,类似于替换渲染的 JSX
321+
* 过去创建的事件处理函数拥有的是创建它们的那次渲染中的 state 值。
321322

322323
</Recap>
323324

324325

325326

326327
<Challenges>
327328

328-
#### Implement a traffic light {/*implement-a-traffic-light*/}
329+
### 实现红绿灯组件 {/*implement-a-traffic-light*/}
329330

330-
Here is a crosswalk light component that toggles on when the button is pressed:
331+
以下是一个人行道红绿灯组件,在按下按钮时会切换状态:
331332

332333
<Sandpack>
333334

@@ -362,13 +363,13 @@ h1 { margin-top: 20px; }
362363

363364
</Sandpack>
364365

365-
Add an `alert` to the click handler. When the light is green and says "Walk", clicking the button should say "Stop is next". When the light is red and says "Stop", clicking the button should say "Walk is next".
366+
向 click 事件处理函数添加一个 `alert` 。当灯为绿色且显示“Walk”时,单击按钮应显示“Stop is next”。当灯为红色并显示“Stop”时,单击按钮应显示“Walk is next”。
366367

367-
Does it make a difference whether you put the `alert` before or after the `setWalk` call?
368+
`alert` 方法放在 `setWalk` 方法之前或之后有区别吗?
368369

369370
<Solution>
370371

371-
Your `alert` should look like this:
372+
你的 `alert` 看起来应该像是这样:
372373

373374
<Sandpack>
374375

@@ -404,17 +405,17 @@ h1 { margin-top: 20px; }
404405

405406
</Sandpack>
406407

407-
Whether you put it before or after the `setWalk` call makes no difference. That render's value of `walk` is fixed. Calling `setWalk` will only change it for the *next* render, but will not affect the event handler from the previous render.
408+
无论你是将它放在 `setWalk` 调用之前还是之后都不会有区别。那次渲染的 `walk` 的值是固定的。调用`setWalk` 只会为 *下次* 渲染对它进行变更,而不会影响来自上次渲染的事件处理函数。
408409

409-
This line might seem counter-intuitive at first:
410+
这一行代码乍看之下似乎有违直觉:
410411

411412
```js
412413
alert(walk ? 'Stop is next' : 'Walk is next');
413414
```
414415

415-
But it makes sense if you read it as: "If the traffic light shows 'Walk now', the message should say 'Stop is next.'" The `walk` variable inside your event handler matches that render's value of `walk` and does not change.
416+
但如果你将其读作“如果交通灯显示‘现在就走吧’,则消息应显示‘接下来是停’。”就容易理解了。你事件处理函数中的 `walk` 变量与所渲染的 `walk` 值是一致的,并且不会发生改变。
416417

417-
You can verify that this is correct by applying the substitution method. When `walk` is `true`, you get:
418+
你可以用替代法来验证这是否正确。当 `walk` `true` 时,你会得到:
418419

419420
```js
420421
<button onClick={() => {
@@ -428,7 +429,7 @@ You can verify that this is correct by applying the substitution method. When `w
428429
</h1>
429430
```
430431

431-
So clicking "Change to Stop" queues a render with `walk` set to `false`, and alerts "Stop is next".
432+
因此,单击 “Change to Stop” 就会入队一次将 `walk` 设置为 `false` 的渲染,并弹出显示 “Stop is next” 的提示框。
432433

433434
</Solution>
434435

0 commit comments

Comments
 (0)
Please sign in to comment.