Skip to content

Commit dd55f92

Browse files
authored
Merge pull request #685 from reactjs/tr/cloneElement
Translate "cloneElement"
2 parents fbd04ed + 19e8012 commit dd55f92

File tree

1 file changed

+51
-51
lines changed

1 file changed

+51
-51
lines changed

src/content/reference/react/cloneElement.md

Lines changed: 51 additions & 51 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` を基に、異なる `props` `children` を持った React 要素を作成します。
3030

3131
```js
3232
import { cloneElement } from 'react';
@@ -43,42 +43,42 @@ const clonedElement = cloneElement(
4343
console.log(clonedElement); // <Row title="Cabbage" isHighlighted={true}>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` オブジェクト内のすべての項目について、返される要素では `element.props` の値よりも `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 要素、文字列、数値、[ポータル](/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 要素オブジェクトを返します。
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+
通常、この要素をコンポーネントから返すか、他の要素の子として用います。要素のプロパティを読み取ることは可能ですが、作成後は要素の構造を非公開 (opaque) として扱い、レンダーのみ行うようにするべきです。
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+
* **複数の子の内容がすべて静的に分かっている場合**`cloneElement` には子を `cloneElement(element, null, child1, child2, child3)` のように**複数の引数として渡してください**。子が動的な場合は、配列全体を第 3 引数として `cloneElement(element, null, listItems)` のように渡してください。これにより、React は動的なリストに `key` が欠けている場合に[警告を出す](/learn/rendering-lists#keeping-list-items-in-order-with-key)ようになります。静的なリストでは並び替えは決して発生しないため、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 要素</CodeStep> の props を上書きするには、それを `cloneElement` に渡し、<CodeStep step={2}>上書きしたい props</CodeStep> を指定します。
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}>クローンされた要素</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+
選択可能な行のリストと、選択されている行を変更する "Next" ボタンをレンダーする `List` コンポーネントを想像してみてください。`List` コンポーネントは、選択された `Row` を異なる方法でレンダーする必要があるため、受け取ったすべての `<Row>` をクローンし、`isHighlighted: true` または `isHighlighted: false` を追加の props として指定します。
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+
子要素をクローンすることで、`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+
"Next" を押すと `List` の state が更新され、異なる行がハイライトされることに着目してください。
141141
142142
<Sandpack>
143143
@@ -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+
子要素をクローンすると、データがアプリケーションを通じてどのように流れるかを把握するのが難しくなります。[代替手段](#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+
### レンダープロップを用いてデータを渡す {/*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+
`cloneElement` を使用する代わりに、`renderItem` のような*レンダープロップ (render prop)* を受け取るようにすることを検討してみてください。以下の例では、`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` のようなものは「レンダープロップ」と呼ばれます。何かをレンダーする方法を指定するための props だからです。例えば、与えられた `isHighlighted` の値で `<Row>` をレンダーする `renderItem` の実装を渡すことができます。
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
@@ -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+
### コンテクストでデータを渡す {/*passing-data-through-context*/}
397397
398-
Another alternative to `cloneElement` is to [pass data through context.](/learn/passing-data-deeply-with-context)
398+
`cloneElement` の別の代替手段として[コンテクストを通じてデータを渡す](/learn/passing-data-deeply-with-context)ことが可能です。
399399
400400
401-
For example, you can call [`createContext`](/reference/react/createContext) to define a `HighlightContext`:
401+
例として、`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` コンポーネントは、レンダーするすべてのアイテムを `HighlightContext` プロバイダでラップします。
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` は props で `isHighlighted` を受け取る必要が一切ありません。代わりにコンテクストから読み取ります。
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+
これにより、呼び出し元のコンポーネントは `<Row>``isHighlighted` を渡すことについて知る必要も、気にする必要もなくなります。
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
@@ -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+
[コンテクストを通じてデータを深く渡す方法について詳しく学ぶ](/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+
### ロジックをカスタムフックに抽出する {/*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+
試すべき別のアプローチは、「非視覚的」なロジックを自前のフックに抽出し、フックから返される情報を使用して何をレンダーするかを決定することです。例えば次のような `useList` カスタムフックを書くことができます。
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() {
@@ -598,7 +598,7 @@ export default function App() {
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+
データフローは明示的ですが、state は任意のコンポーネントから使用できる `useList` カスタムフック内にあります。
602602
603603
<Sandpack>
604604
@@ -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)