From 5f08311b4fc14ab969f2b36f01dec0c35b9a6389 Mon Sep 17 00:00:00 2001
From: qinhua <352484005@qq.com>
Date: Mon, 8 Nov 2021 20:30:24 +0800
Subject: [PATCH 1/7] docs(cn): sharing-state-between-components
---
.../learn/sharing-state-between-components.md | 202 +++++++++---------
1 file changed, 102 insertions(+), 100 deletions(-)
diff --git a/beta/src/pages/learn/sharing-state-between-components.md b/beta/src/pages/learn/sharing-state-between-components.md
index 43aa9ceffc..d12bc3924a 100644
--- a/beta/src/pages/learn/sharing-state-between-components.md
+++ b/beta/src/pages/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.
+有时候,你希望两个组件的状态始终同步更改。要实现这一点,可以将相关状态从这两个组件上移除,并把状态放到它们的公共父级,再通过 props 将状态传递给这两个组件。这被称为“状态提升”,这是编写 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}
-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,23 +75,23 @@ h3, p { margin: 5px 0px; }
-Notice how pressing one panel's button does not affect the other panel--they are independent.
+我们发现点击其中一个面板中的按钮并不会影响另外一个——他们是独立的。
-**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?
+**假设现在你想改变这种行为,保证同一时间只展开 1 个面板。** 基于这种设计,展开第 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. 从子组件中 **移除** 状态。
+2. 从父组件 **传递** 硬编码数据
+3. 为父组件添加状态并将其与事件处理函数一起向下传递
-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
+### 第 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` 的控制权交给他们的父组件。然后再把这个状态通过 `props` 传给子组件。我们先从 `Panel` 组件中 **删除这一行**:
```js
const [isActive, setIsActive] = useState(false);
@@ -101,17 +103,17 @@ And instead, add `isActive` to the `Panel`'s list of props:
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
+### 第 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` )传递给两个面板:
@@ -121,12 +123,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 被认为是现今苹果的祖先。
>
);
@@ -140,7 +142,7 @@ function Panel({ title, children, isActive }) {
{children}
) : (
)}
@@ -158,23 +160,23 @@ h3, p { margin: 5px 0px; }
-Try editing the hardcoded `isActive` values in the `Accordion` component and see the result on the screen.
+尝试修改 `Accordion` 组件中 `isActive` 的值,并在屏幕上查看结果。
-### Step 3: Add state to the common parent
+### 第 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 an 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
<>
@@ -193,7 +195,7 @@ Clicking the "Show" button in either `Panel` needs to change the active index in
>
```
-The `
diff --git a/beta/src/content/learn/sharing-state-between-components.md b/beta/src/content/learn/sharing-state-between-components.md
index b2477160d2..f8b83c2e4a 100644
--- a/beta/src/content/learn/sharing-state-between-components.md
+++ b/beta/src/content/learn/sharing-state-between-components.md
@@ -325,7 +325,7 @@ h3, p { margin: 5px 0px; }
-### 同步输入状态 {/*synced-inputs*/}
+#### 同步输入状态 {/*synced-inputs*/}
现在有两个独立的输入框。为了让它们保持同步:即编辑一个输入框时,另一个输入框也会更新相同的文本,反之亦然。
@@ -431,7 +431,7 @@ label { display: block; }
-### 列表过滤 {/*filtering-a-list*/}
+#### 列表过滤 {/*filtering-a-list*/}
在这个例子中,`SearchBar` 组件拥有一个用来控制输入框的 `query` 状态,它的父组件中展示了一个 `List` 组件,但是没有考虑搜索条件。
diff --git a/beta/src/sidebarLearn.json b/beta/src/sidebarLearn.json
index 90fed7e260..ebef19a628 100644
--- a/beta/src/sidebarLearn.json
+++ b/beta/src/sidebarLearn.json
@@ -139,7 +139,7 @@
"path": "/learn/choosing-the-state-structure"
},
{
- "title": "组件间共享状态",
+ "title": "在组件间共享状态",
"path": "/learn/sharing-state-between-components"
},
{
From 99b7118bed860461460835ab33345b97fac9653b Mon Sep 17 00:00:00 2001
From: Baby Chin <352484005@qq.com>
Date: Mon, 20 Feb 2023 11:34:47 +0800
Subject: [PATCH 6/7] Update
beta/src/content/learn/sharing-state-between-components.md
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
@daochouwangu 确认一下,现在翻译的时候,英文还要加行内代码符号吗?
Co-authored-by: TimLi
---
beta/src/content/learn/sharing-state-between-components.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/beta/src/content/learn/sharing-state-between-components.md b/beta/src/content/learn/sharing-state-between-components.md
index f8b83c2e4a..ec0aa8f162 100644
--- a/beta/src/content/learn/sharing-state-between-components.md
+++ b/beta/src/content/learn/sharing-state-between-components.md
@@ -308,7 +308,7 @@ h3, p { margin: 5px 0px; }
在 `React` 应用中,很多组件都有自己的状态。一些状态可能“活跃”在叶子组件(树形结构最底层的组件)附近,例如输入框。另一些状态可能在应用程序顶部“活动”。例如,客户端路由库也是通过将当前路由存储在 `React` 状态中,利用 `props` 将状态层层传递下去来实现的!
-**对于每个独一无二的状态,都应该存在拥有该状态的组件**。这一原则也被称为拥有 [“可信单一数据源”](https://en.wikipedia.org/wiki/Single_source_of_truth)。它并不意味着所有状态都活跃在一个地方——对每个状态来说,都需要一个特定的组件来保存这些状态信息。你应该 *将状态提升* 到公共父级,或 *将状态传递* 到需要它的子级中,而不是在组件之间复制共享的状态。
+**对于每个独特的状态,都应该存在且只存在于一个指定的组件中作为 state **。这一原则也被称为拥有 [“可信单一数据源”](https://en.wikipedia.org/wiki/Single_source_of_truth)。它并不意味着所有状态都存在一个地方——对每个状态来说,都需要一个特定的组件来保存这些状态信息。你应该 *将状态提升* 到公共父级,或 *将状态传递* 到需要它的子级中,而不是在组件之间复制共享的状态。
你的应用会随着你的操作而变化。当你将状态上下移动时,你依然会想要确定每个状态在哪里“活跃”。这都是过程的一部分!
From 4264898bfb33381c846e3cb3a930407762c9971f Mon Sep 17 00:00:00 2001
From: Xavi Lee
Date: Sat, 11 Mar 2023 08:34:11 +0800
Subject: [PATCH 7/7] Apply suggestions from code review
Co-authored-by: TimLi
---
.../learn/sharing-state-between-components.md | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/beta/src/content/learn/sharing-state-between-components.md b/beta/src/content/learn/sharing-state-between-components.md
index ec0aa8f162..8190241236 100644
--- a/beta/src/content/learn/sharing-state-between-components.md
+++ b/beta/src/content/learn/sharing-state-between-components.md
@@ -6,7 +6,7 @@ translators:
-有时候,你希望两个组件的状态始终同步更改。要实现这一点,可以将相关状态从这两个组件上移除,并把状态放到它们的公共父级,再通过 props 将状态传递给这两个组件。这被称为“状态提升”,这是编写 React 代码时常做的事。
+有时候,你希望两个组件的状态始终同步更改。要实现这一点,可以将相关 state 从这两个组件上移除,并把 state 放到它们的公共父级,再通过 props 将 state 传递给这两个组件。这被称为“状态提升”,这是编写 React 代码时常做的事。
@@ -98,9 +98,9 @@ h3, p { margin: 5px 0px; }
要协调好这两个面板,我们需要分 3 步将状态“提升”到他们的父组件中。
-1. 从子组件中 **移除** 状态。
+1. 从子组件中 **移除** state 。
2. 从父组件 **传递** 硬编码数据。
-3. 为共同的父组件添加状态,并将其与事件处理函数一起向下传递。
+3. 为共同的父组件添加 state ,并将其与事件处理函数一起向下传递。
这样,`Accordion` 组件就可以控制 2 个 `Panel` 组件,保证同一时间只能展开一个。
@@ -316,10 +316,10 @@ h3, p { margin: 5px 0px; }
-* 当你想要整合两个组件时,将它们的状态移动到共同的父组件中。
+* 当你想要整合两个组件时,将它们的 state 移动到共同的父组件中。
* 然后在父组件中通过 `props` 把信息传递下去。
-* 最后,向下传递事件处理程序,以便子组件可以改变父组件的状态。
-* 考虑该将组件视为“受控”(由属性驱动)或是“不受控”(由状态驱动)是十分有益的。
+* 最后,向下传递事件处理程序,以便子组件可以改变父组件的 state 。
+* 考虑该将组件视为“受控”(由 prop 驱动)或是“不受控”(由 state 驱动)是十分有益的。