diff --git a/beta/src/content/learn/sharing-state-between-components.md b/beta/src/content/learn/sharing-state-between-components.md
index cc98d8f82b..8190241236 100644
--- a/beta/src/content/learn/sharing-state-between-components.md
+++ b/beta/src/content/learn/sharing-state-between-components.md
@@ -1,31 +1,33 @@
---
-title: Sharing State Between Components
+title: 在组件间共享状态
+translators:
+ - qinhua
---
-Sometimes, you want the state of two components to always change together. To do it, remove state from both of them, move it to their closest common parent, and then pass it down to them via props. This is known as *lifting state up,* and it's one of the most common things you will do writing React code.
+有时候,你希望两个组件的状态始终同步更改。要实现这一点,可以将相关 state 从这两个组件上移除,并把 state 放到它们的公共父级,再通过 props 将 state 传递给这两个组件。这被称为“状态提升”,这是编写 React 代码时常做的事。
-- How to share state between components by lifting it up
-- What are controlled and uncontrolled components
+- 如何使用状态提升在组件之间共享状态
+- 什么是受控组件和非受控组件
-## Lifting state up by example {/*lifting-state-up-by-example*/}
+## 举例说明一下状态提升 {/*lifting-state-up-by-example*/}
-In this example, a parent `Accordion` component renders two separate `Panel`s:
+在这个例子中,父组件 `Accordion` 渲染了 2 个独立的 `Panel` 组件。
* `Accordion`
- `Panel`
- `Panel`
-Each `Panel` component has a boolean `isActive` state that determines whether its content is visible.
+每个 `Panel` 组件都有一个布尔值 `isActive`,用于控制其内容是否可见。
-Press the Show button for both panels:
+请点击 2 个面板中的显示按钮:
@@ -41,7 +43,7 @@ function Panel({ title, children }) {
{children}
) : (
)}
@@ -51,12 +53,12 @@ function Panel({ title, children }) {
export default function Accordion() {
return (
<>
-
Almaty, Kazakhstan
-
- With a population of about 2 million, Almaty is Kazakhstan's largest city. From 1929 to 1997, it was its capital city.
+
哈萨克斯坦,阿拉木图
+
+ 阿拉木图人口约200万,是哈萨克斯坦最大的城市。它在 1929 年到 1997 年间都是首都。
-
- The name comes from алма, the Kazakh word for "apple" and is often translated as "full of apples". In fact, the region surrounding Almaty is thought to be the ancestral home of the apple, and the wild Malus sieversii is considered a likely candidate for the ancestor of the modern domestic apple.
+
+ 这个名字来自于 алма,哈萨克语中“苹果”的意思,经常被翻译成“苹果之乡”。事实上,阿拉木图的周边地区被认为是苹果的发源地,Malus sieversii 被认为是现今苹果的祖先。
>
);
@@ -73,59 +75,60 @@ h3, p { margin: 5px 0px; }
-Notice how pressing one panel's button does not affect the other panel--they are independent.
+我们发现点击其中一个面板中的按钮并不会影响另外一个,他们是独立的。
+
-
+
-Initially, each `Panel`'s `isActive` state is `false`, so they both appear collapsed
+由于一开始所有 `Panel` 内部的 `isActive` 都是 `false`,因此它们的内容都不会显示。
-
+
-Clicking either `Panel`'s button will only update that `Panel`'s `isActive` state alone
+此时,点击任意一个 `Panel` 组件中的按钮都只会更新当前 `Panel` 组件内的 `isActive` 值
-**But now let's say you want to change it so that only one panel is expanded at any given time.** With that design, expanding the second panel should collapse the first one. How would you do that?
+**假设现在您想改变这种行为,以便在任何时候只展开一个面板。** 在这种设计下,展开第 2 个面板应会折叠第 1 个面板。您该如何做到这一点呢?"
-To coordinate these two panels, you need to "lift their state up" to a parent component in three steps:
+要协调好这两个面板,我们需要分 3 步将状态“提升”到他们的父组件中。
-1. **Remove** state from the child components.
-2. **Pass** hardcoded data from the common parent.
-3. **Add** state to the common parent and pass it down together with the event handlers.
+1. 从子组件中 **移除** state 。
+2. 从父组件 **传递** 硬编码数据。
+3. 为共同的父组件添加 state ,并将其与事件处理函数一起向下传递。
-This will allow the `Accordion` component to coordinate both `Panel`s and only expand one at a time.
+这样,`Accordion` 组件就可以控制 2 个 `Panel` 组件,保证同一时间只能展开一个。
-### Step 1: Remove state from the child components {/*step-1-remove-state-from-the-child-components*/}
+### 第 1 步: 从子组件中移除状态 {/*step-1-remove-state-from-the-child-components*/}
-You will give control of the `Panel`'s `isActive` to its parent component. This means that the parent component will pass `isActive` to `Panel` as a prop instead. Start by **removing this line** from the `Panel` component:
+你将把 `Panel` 组件对 `isActive` 的控制权交给他们的父组件。这意味着,父组件会将 `isActive` 作为 `prop` 传给子组件 `Panel`。我们先从 `Panel` 组件中 **删除下面这一行**:
```js
const [isActive, setIsActive] = useState(false);
```
-And instead, add `isActive` to the `Panel`'s list of props:
+然后,把 `isActive` 加入 `Panel` 组件的 `props` 中:
```js
function Panel({ title, children, isActive }) {
```
-Now the `Panel`'s parent component can *control* `isActive` by [passing it down as a prop.](/learn/passing-props-to-a-component) Conversely, the `Panel` component now has *no control* over the value of `isActive`--it's now up to the parent component!
+现在 `Panel` 的父组件就可以通过 [向下传递 prop](/learn/passing-props-to-a-component) 来 *控制* `isActive`。但相反地,`Panel` 组件对 `isActive` 的值 *没有控制权* —— 现在完全由父组件决定!
-### Step 2: Pass hardcoded data from the common parent {/*step-2-pass-hardcoded-data-from-the-common-parent*/}
+### 第 2 步: 从公共父组件传递硬编码数据 {/*step-2-pass-hardcoded-data-from-the-common-parent*/}
-To lift state up, you must locate the closest common parent component of *both* of the child components that you want to coordinate:
+为了实现状态提升,必须定位到你想协调的 *两个* 子组件最近的公共父组件:
-* `Accordion` *(closest common parent)*
+* `Accordion` *(最近的公共父组件)*
- `Panel`
- `Panel`
-In this example, it's the `Accordion` component. Since it's above both panels and can control their props, it will become the "source of truth" for which panel is currently active. Make the `Accordion` component pass a hardcoded value of `isActive` (for example, `true`) to both panels:
+在这个例子中,公共父组件是 `Accordion`。因为它位于两个面板之上,可以控制它们的 props,所以它将成为当前激活面板的“控制之源”。通过 `Accordion` 组件将硬编码值 `isActive`(例如 `true` )传递给两个面板:
@@ -135,12 +138,12 @@ import { useState } from 'react';
export default function Accordion() {
return (
<>
-
Almaty, Kazakhstan
-
- With a population of about 2 million, Almaty is Kazakhstan's largest city. From 1929 to 1997, it was its capital city.
+
哈萨克斯坦,阿拉木图
+
+ 阿拉木图人口约200万,是哈萨克斯坦最大的城市。它在 1929 年到 1997 年间都是首都。
-
- The name comes from алма, the Kazakh word for "apple" and is often translated as "full of apples". In fact, the region surrounding Almaty is thought to be the ancestral home of the apple, and the wild Malus sieversii is considered a likely candidate for the ancestor of the modern domestic apple.
+
+ 这个名字来自于 алма,哈萨克语中“苹果”的意思,经常被翻译成“苹果之乡”。事实上,阿拉木图的周边地区被认为是苹果的发源地,Malus sieversii 被认为是现今苹果的祖先。
>
);
@@ -154,7 +157,7 @@ function Panel({ title, children, isActive }) {
{children}
) : (
)}
@@ -172,21 +175,22 @@ h3, p { margin: 5px 0px; }
-Try editing the hardcoded `isActive` values in the `Accordion` component and see the result on the screen.
+你可以尝试修改 `Accordion` 组件中 `isActive` 的值,并在屏幕上查看结果。
+
+### 第 3 步: 为公共父组件添加状态 {/*step-3-add-state-to-the-common-parent*/}
-### Step 3: Add state to the common parent {/*step-3-add-state-to-the-common-parent*/}
+状态提升通常会改变原状态的数据存储类型。
-Lifting state up often changes the nature of what you're storing as state.
-In this case, only one panel should be active at a time. This means that the `Accordion` common parent component needs to keep track of *which* panel is the active one. Instead of a `boolean` value, it could use a number as the index of the active `Panel` for the state variable:
+在这个例子中,一次只能激活一个面板。这意味着 `Accordion` 这个父组件需要记录 *哪个* 面板是被激活的面板。我们可以用数字作为当前被激活 `Panel` 的索引,而不是 `boolean` 值:
```js
const [activeIndex, setActiveIndex] = useState(0);
```
-When the `activeIndex` is `0`, the first panel is active, and when it's `1`, it's the second one.
+当 `activeIndex` 为 `0` 时,激活第一个面板,为 `1` 时,激活第二个面板。
-Clicking the "Show" button in either `Panel` needs to change the active index in `Accordion`. A `Panel` can't set the `activeIndex` state directly because it's defined inside the `Accordion`. The `Accordion` component needs to *explicitly allow* the `Panel` component to change its state by [passing an event handler down as a prop](/learn/responding-to-events#passing-event-handlers-as-props):
+在任意一个 `Panel` 中点击“显示”按钮都需要更改 `Accordion` 中的激活索引值。 `Panel` 中无法直接设置状态 `activeIndex` 的值,因为该状态是在 `Accordion` 组件内部定义的。 `Accordion` 组件需要 *显式允许* `Panel` 组件通过 [将事件处理程序作为 prop 向下传递](/learn/responding-to-events#passing-event-handlers-as-props) 来更改其状态:
```js
<>
@@ -205,7 +209,7 @@ Clicking the "Show" button in either `Panel` needs to change the active index in
>
```
-The `