Skip to content

Commit 125770b

Browse files
li-jia-nanYucohny
andauthored
docs(cn): translate reference/react/cloneElement into Chinese (#1207)
Co-authored-by: Yucohny <yucohny@163.com>
1 parent 815a2c1 commit 125770b

File tree

1 file changed

+56
-56
lines changed

1 file changed

+56
-56
lines changed

src/content/reference/react/cloneElement.md

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ title: cloneElement
44

55
<Pitfall>
66

7-
Using `cloneElement` is uncommon and can lead to fragile code. [See common alternatives.](#alternatives)
7+
使用 `cloneElement` 并不常见,并且可能会导致代码变得脆弱。[查看常见的替代方案](#alternatives)
88

99
</Pitfall>
1010

1111
<Intro>
1212

13-
`cloneElement` lets you create a new React element using another element as a starting point.
13+
`cloneElement` 允许你使用一个元素作为初始值创建一个新的 React 元素。
1414

1515
```js
1616
const clonedElement = cloneElement(element, props, ...children)
@@ -22,11 +22,11 @@ const clonedElement = cloneElement(element, props, ...children)
2222

2323
---
2424

25-
## Reference {/*reference*/}
25+
## 参考 {/*reference*/}
2626

2727
### `cloneElement(element, props, ...children)` {/*cloneelement*/}
2828

29-
Call `cloneElement` to create a React element based on the `element`, but with different `props` and `children`:
29+
调用 `cloneElement` 方法会基于 `element` 创建一个新的 React 元素,但新元素具有不同的 `props` `children`
3030

3131
```js
3232
import { cloneElement } from 'react';
@@ -43,42 +43,42 @@ const clonedElement = cloneElement(
4343
console.log(clonedElement); // <Row title="Cabbage">Goodbye</Row>
4444
```
4545

46-
[See more examples below.](#usage)
46+
[请参阅下面的更多示例](#usage)
4747

48-
#### Parameters {/*parameters*/}
48+
#### 参数 {/*parameters*/}
4949

50-
* `element`: The `element` argument must be a valid React element. For example, it could be a JSX node like `<Something />`, the result of calling [`createElement`](/reference/react/createElement), or the result of another `cloneElement` call.
50+
* `element``element` 参数必须是一个有效的 React 元素。例如,它可以是一个类似 `<Something />` 这样的 JSX 节点,也可以是 [`createElement`](/reference/react/createElement) 调用的结果,或者也可以是另一个 `cloneElement` 调用的结果。
5151

52-
* `props`: The `props` argument must either be an object or `null`. If you pass `null`, the cloned element will retain all of the original `element.props`. Otherwise, for every prop in the `props` object, the returned element will "prefer" the value from `props` over the value from `element.props`. The rest of the props will be filled from the original `element.props`. If you pass `props.key` or `props.ref`, they will replace the original ones.
52+
* `props``props` 参数必须是一个对象或 `null`。如果传 `null`,克隆后的元素将保留所有原始的 `element.props`。否则,对于 `props` 对象中的每个 prop 属性,返回的元素将“优先”使用 `props` 中的值而不是 `element.props` 中的值。其余的 props 将从原始的 `element.props` 中填充。如果你传递 `props.key` 或者 `props.ref`,它们将替换原来的。
5353

54-
* **optional** `...children`: Zero or more child nodes. They can be any React nodes, including React elements, strings, numbers, [portals](/reference/react-dom/createPortal), empty nodes (`null`, `undefined`, `true`, and `false`), and arrays of React nodes. If you don't pass any `...children` arguments, the original `element.props.children` will be preserved.
54+
* **可选** `...children`:零个或多个子节点。它们可以是任何 React 节点,包括 React 元素、字符串、数字、[portals](/reference/react-dom/createPortal)、空节点(`null``undefined``true` `false`),和 React 元素数组。如果你不传递任何 `...children` 参数,则原始的 `element.props.children` 将被保留。
5555

56-
#### Returns {/*returns*/}
56+
#### 返回值 {/*returns*/}
5757

58-
`cloneElement` returns a React element object with a few properties:
58+
`cloneElement` 返回一个具有一些属性的 React element 对象:
5959

60-
* `type`: Same as `element.type`.
61-
* `props`: The result of shallowly merging `element.props` with the overriding `props` you have passed.
62-
* `ref`: The original `element.ref`, unless it was overridden by `props.ref`.
63-
* `key`: The original `element.key`, unless it was overridden by `props.key`.
60+
* `type`:与 `element.type` 相同。
61+
* `props`:将 `element.props` 与你传递的 `props` 浅合并的结果。
62+
* `ref`:原始的 `element.ref`,除非它被 `props.ref` 覆盖。
63+
* `key`:原始的 `element.key`,除非它被 `props.key` 覆盖。
6464

65-
Usually, you'll return the element from your component or make it a child of another element. Although you may read the element's properties, it's best to treat every element as opaque after it's created, and only render it.
65+
通常,你将从组件返回该元素或使其成为另一个元素的子元素。尽管你可以读取元素的属性,但最好在创建每个元素后将其视为不透明的,并且仅渲染它。
6666

67-
#### Caveats {/*caveats*/}
67+
#### 注意事项 {/*caveats*/}
6868

69-
* Cloning an element **does not modify the original element.**
69+
* 克隆一个元素 **不会修改原始元素**
7070

71-
* You should only **pass children as multiple arguments to `cloneElement` if they are all statically known,** like `cloneElement(element, null, child1, child2, child3)`. If your children are dynamic, pass the entire array as the third argument: `cloneElement(element, null, listItems)`. This ensures that React will [warn you about missing `key`s](/learn/rendering-lists#keeping-list-items-in-order-with-key) for any dynamic lists. For static lists this is not necessary because they never reorder.
71+
* 如果已知 children 是静态的,则你应该 **将它们作为多个参数传递给 `cloneElement`**,例如 `cloneElement(element, null, child1, child2, child3)`。如果你的 children 是动态的,请将整个数组作为第三个参数传递:`cloneElement(element, null, listItems)`。这确保了 React 会对任何动态列表 [警告你缺少“key](/learn/rendering-lists#keeping-list-items-in-order-with-key),对于静态的列表,这是不必要的,因为它们不会重新排序。
7272

73-
* `cloneElement` makes it harder to trace the data flow, so **try the [alternatives](#alternatives) instead.**
73+
* `cloneElement` 会使得跟踪数据流向变得更加困难,所以请 **尝试使用 [替代方案](#alternatives)**
7474

7575
---
7676

77-
## Usage {/*usage*/}
77+
## 用法 {/*usage*/}
7878

79-
### Overriding props of an element {/*overriding-props-of-an-element*/}
79+
### 覆盖元素的 props {/*overriding-props-of-an-element*/}
8080

81-
To override the props of some <CodeStep step={1}>React element</CodeStep>, pass it to `cloneElement` with the <CodeStep step={2}>props you want to override</CodeStep>:
81+
要覆盖某些 <CodeStep step={1}>React element</CodeStep> 的 props,请将其与 <CodeStep step={2}>要覆盖的 props</CodeStep> 一起传递给 `cloneElement`
8282

8383
```js [[1, 5, "<Row title=\\"Cabbage\\" />"], [2, 6, "{ isHighlighted: true }"], [3, 4, "clonedElement"]]
8484
import { cloneElement } from 'react';
@@ -90,11 +90,11 @@ const clonedElement = cloneElement(
9090
);
9191
```
9292

93-
Here, the resulting <CodeStep step={3}>cloned element</CodeStep> will be `<Row title="Cabbage" isHighlighted={true} />`.
93+
在这里,生成的 <CodeStep step={3}>克隆 element</CodeStep> 将为 `<Row title="Cabbage" isHighlighted={true} />`
9494

95-
**Let's walk through an example to see when it's useful.**
95+
**让我们看一个示例,看看它什么时候有用**
9696

97-
Imagine a `List` component that renders its [`children`](/learn/passing-props-to-a-component#passing-jsx-as-children) as a list of selectable rows with a "Next" button that changes which row is selected. The `List` component needs to render the selected `Row` differently, so it clones every `<Row>` child that it has received, and adds an extra `isHighlighted: true` or `isHighlighted: false` prop:
97+
想象一个 `List` 组件将其 [`children`](/learn/passing-props-to-a-component#passing-jsx-as-children) 渲染为可选择行的列表,并带有可更改的“下一步”按钮选择了哪一行。`List` 组件需要以不同的方式渲染所选的 `Row`,因此它克隆它收到的每个 `<Row>` 子级,并添加额外的 `isHighlighted: true` `isHighlighted: false` 属性:
9898

9999
```js {6-8}
100100
export default function List({ children }) {
@@ -108,7 +108,7 @@ export default function List({ children }) {
108108
)}
109109
```
110110
111-
Let's say the original JSX received by `List` looks like this:
111+
假设 `List` 收到的原始 JSX 如下所示:
112112
113113
```js {2-4}
114114
<List>
@@ -118,7 +118,7 @@ Let's say the original JSX received by `List` looks like this:
118118
</List>
119119
```
120120
121-
By cloning its children, the `List` can pass extra information to every `Row` inside. The result looks like this:
121+
通过克隆其 children`List` 可以将额外的信息传递给内部的每个 `Row`。结果如下:
122122
123123
```js {4,8,12}
124124
<List>
@@ -137,7 +137,7 @@ By cloning its children, the `List` can pass extra information to every `Row` in
137137
</List>
138138
```
139139
140-
Notice how pressing "Next" updates the state of the `List`, and highlights a different row:
140+
注意点击“下一步”如何更新 `List` 的状态,并高亮显示不同的行:
141141
142142
<Sandpack>
143143
@@ -178,7 +178,7 @@ export default function List({ children }) {
178178
(i + 1) % Children.count(children)
179179
);
180180
}}>
181-
Next
181+
下一步
182182
</button>
183183
</div>
184184
);
@@ -232,21 +232,21 @@ button {
232232
233233
</Sandpack>
234234
235-
To summarize, the `List` cloned the `<Row />` elements it received and added an extra prop to them.
235+
总而言之,`List` 克隆了它接收的 `<Row />` 元素,并向它们添加额外的 props。
236236
237237
<Pitfall>
238238
239-
Cloning children makes it hard to tell how the data flows through your app. Try one of the [alternatives.](#alternatives)
239+
克隆 children 使得你很难判断数据如何流经你的应用。尝试一种 [替代方案](#alternatives)
240240
241241
</Pitfall>
242242
243243
---
244244
245-
## Alternatives {/*alternatives*/}
245+
## 替代方案 {/*alternatives*/}
246246
247-
### Passing data with a render prop {/*passing-data-with-a-render-prop*/}
247+
### 通过 props 传递数据 {/*passing-data-with-a-render-prop*/}
248248
249-
Instead of using `cloneElement`, consider accepting a *render prop* like `renderItem`. Here, `List` receives `renderItem` as a prop. `List` calls `renderItem` for every item and passes `isHighlighted` as an argument:
249+
接受类似 `renderItem` 这样的 *render prop* 代替 `cloneElement` 的用法。在这里,`List` 接收 `renderItem` 作为 props。`List` 为数组每一项调用 `renderItem`,并传递 `isHighlighted` 作为参数:
250250
251251
```js {1,7}
252252
export default function List({ items, renderItem }) {
@@ -259,7 +259,7 @@ export default function List({ items, renderItem }) {
259259
})}
260260
```
261261
262-
The `renderItem` prop is called a "render prop" because it's a prop that specifies how to render something. For example, you can pass a `renderItem` implementation that renders a `<Row>` with the given `isHighlighted` value:
262+
`renderItem` 属性称为“渲染属性”,因为它是决定如何渲染某些内容的属性。例如,你可以传递一个 `renderItem` 实现使用给定的 `isHighlighted` 值呈现 `<Row>`
263263
264264
```js {3,7}
265265
<List
@@ -274,7 +274,7 @@ The `renderItem` prop is called a "render prop" because it's a prop that specifi
274274
/>
275275
```
276276
277-
The end result is the same as with `cloneElement`:
277+
最终结果与 `cloneElement` 相同:
278278
279279
```js {4,8,12}
280280
<List>
@@ -293,7 +293,7 @@ The end result is the same as with `cloneElement`:
293293
</List>
294294
```
295295
296-
However, you can clearly trace where the `isHighlighted` value is coming from.
296+
但是你可以清楚地追踪 `isHighlighted` 的来源。
297297
298298
<Sandpack>
299299
@@ -335,7 +335,7 @@ export default function List({ items, renderItem }) {
335335
(i + 1) % items.length
336336
);
337337
}}>
338-
Next
338+
下一步
339339
</button>
340340
</div>
341341
);
@@ -389,22 +389,22 @@ button {
389389
390390
</Sandpack>
391391
392-
This pattern is preferred to `cloneElement` because it is more explicit.
392+
这种方案优于 `cloneElement`,因为它更加清晰。
393393
394394
---
395395
396-
### Passing data through context {/*passing-data-through-context*/}
396+
### 通过 context 传递数据 {/*passing-data-through-context*/}
397397
398-
Another alternative to `cloneElement` is to [pass data through context.](/learn/passing-data-deeply-with-context)
398+
`cloneElement` 的另一种替代方法是 [通过 context 传递数据](/learn/passing-data-deeply-with-context)
399399
400400
401-
For example, you can call [`createContext`](/reference/react/createContext) to define a `HighlightContext`:
401+
例如,你可以调用 [`createContext`](/reference/react/createContext) 来定义一个 `HighlightContext`
402402
403403
```js
404404
export const HighlightContext = createContext(false);
405405
```
406406
407-
Your `List` component can wrap every item it renders into a `HighlightContext` provider:
407+
`List` 组件可以将其呈现的每个 item 传递到 `HighlightContext` provider 中:
408408
409409
```js {8,10}
410410
export default function List({ items, renderItem }) {
@@ -421,15 +421,15 @@ export default function List({ items, renderItem }) {
421421
})}
422422
```
423423
424-
With this approach, `Row` does not need to receive an `isHighlighted` prop at all. Instead, it reads the context:
424+
通过这种方法,`Row` 不需要接收 `isHighlighted`属性,因为它可以从 context 中读取:
425425
426426
```js Row.js {2}
427427
export default function Row({ title }) {
428428
const isHighlighted = useContext(HighlightContext);
429429
// ...
430430
```
431431
432-
This allows the calling component to not know or worry about passing `isHighlighted` to `<Row>`:
432+
这允许调用组件时无需关心是否将 `isHighlighted` 传递给了 `<Row>`
433433
434434
```js {4}
435435
<List
@@ -440,7 +440,7 @@ This allows the calling component to not know or worry about passing `isHighligh
440440
/>
441441
```
442442
443-
Instead, `List` and `Row` coordinate the highlighting logic through context.
443+
相反,`List` `Row` 通过上下文协调突出显示逻辑。
444444
445445
<Sandpack>
446446
@@ -486,7 +486,7 @@ export default function List({ items, renderItem }) {
486486
(i + 1) % items.length
487487
);
488488
}}>
489-
Next
489+
下一步
490490
</button>
491491
</div>
492492
);
@@ -550,13 +550,13 @@ button {
550550
551551
</Sandpack>
552552
553-
[Learn more about passing data through context.](/reference/react/useContext#passing-data-deeply-into-the-tree)
553+
[了解有关通过 context 传递数据的更多信息](/reference/react/useContext#passing-data-deeply-into-the-tree)
554554
555555
---
556556
557-
### Extracting logic into a custom Hook {/*extracting-logic-into-a-custom-hook*/}
557+
### 将逻辑提取到自定义 Hook {/*extracting-logic-into-a-custom-hook*/}
558558
559-
Another approach you can try is to extract the "non-visual" logic into your own Hook, and use the information returned by your Hook to decide what to render. For example, you could write a `useList` custom Hook like this:
559+
你可以尝试的另一种方法是将“非视觉”部分的逻辑提取到你的自定义 Hook 中,并使用 Hook 的返回值来决定渲染什么。例如,你可以编写一个 `useList` 自定义 Hook,如下所示:
560560
561561
```js
562562
import { useState } from 'react';
@@ -575,7 +575,7 @@ export default function useList(items) {
575575
}
576576
```
577577
578-
Then you could use it like this:
578+
然后你可以像这样使用它:
579579
580580
```js {2,9,13}
581581
export default function App() {
@@ -591,14 +591,14 @@ export default function App() {
591591
)}
592592
<hr />
593593
<button onClick={onNext}>
594-
Next
594+
下一步
595595
</button>
596596
</div>
597597
);
598598
}
599599
```
600600
601-
The data flow is explicit, but the state is inside the `useList` custom Hook that you can use from any component:
601+
数据流是显式的,但状态位于 `useList` 自定义 Hook 内,你可以在任意一个组件内使用它:
602602
603603
<Sandpack>
604604
@@ -620,7 +620,7 @@ export default function App() {
620620
)}
621621
<hr />
622622
<button onClick={onNext}>
623-
Next
623+
下一步
624624
</button>
625625
</div>
626626
);
@@ -691,4 +691,4 @@ button {
691691
692692
</Sandpack>
693693
694-
This approach is particularly useful if you want to reuse this logic between different components.
694+
如果你想在不同组件之间复用此逻辑,则这个方案十分有用。

0 commit comments

Comments
 (0)