@@ -4,13 +4,13 @@ title: cloneElement
4
4
5
5
<Pitfall >
6
6
7
- Using ` cloneElement ` is uncommon and can lead to fragile code. [ See common alternatives. ] ( #alternatives )
7
+ ` cloneElement ` の使用は一般的ではなく、コードが壊れやすくなる可能性があります。 [ 一般的な代替手段をご覧ください ] ( #alternatives ) 。
8
8
9
9
</Pitfall >
10
10
11
11
<Intro >
12
12
13
- ` cloneElement ` lets you create a new React element using another element as a starting point.
13
+ ` cloneElement ` を使用すると、別の要素に基づいて新しい React 要素を作成することができます。
14
14
15
15
``` js
16
16
const clonedElement = cloneElement (element, props, ... children)
@@ -22,11 +22,11 @@ const clonedElement = cloneElement(element, props, ...children)
22
22
23
23
---
24
24
25
- ## Reference {/* reference* /}
25
+ ## リファレンス {/* reference* /}
26
26
27
27
### ` cloneElement(element, props, ...children) ` {/* cloneelement* /}
28
28
29
- Call ` cloneElement ` to create a React element based on the ` element ` , but with different ` props ` and ` children ` :
29
+ ` cloneElement ` を呼び出して、 ` element ` を基に、異なる ` props ` と ` children ` を持った React 要素を作成します。
30
30
31
31
``` js
32
32
import { cloneElement } from ' react' ;
@@ -43,42 +43,42 @@ const clonedElement = cloneElement(
43
43
console .log (clonedElement); // <Row title="Cabbage" isHighlighted={true}>Goodbye</Row>
44
44
```
45
45
46
- [ See more examples below. ] ( #usage )
46
+ [ さらに例を見る ] ( #usage )
47
47
48
- #### Parameters {/* parameters* /}
48
+ #### 引数 {/* parameters* /}
49
49
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 ` の呼び出し結果などです。
51
51
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 ` を渡した場合、それらは元のものを置き換えます。
53
53
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 ` が保持されます。
55
55
56
- #### Returns {/* returns* /}
56
+ #### 返り値 {/* returns* /}
57
57
58
- ` cloneElement ` returns a React element object with a few properties:
58
+ ` cloneElement ` は以下のプロパティを持つ React 要素オブジェクトを返します。
59
59
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 ` によって上書きされた場合は除く。
64
64
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) として扱い、レンダーのみ行うようにするべきです。
66
66
67
- #### Caveats {/* caveats* /}
67
+ #### 注意点 {/* caveats* /}
68
68
69
- * Cloning an element ** does not modify the original element. **
69
+ * 要素をクローンしても ** 元の要素は変更されません ** 。
70
70
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 は必要ありません。
72
72
73
- * ` cloneElement ` makes it harder to trace the data flow, so ** try the [ alternatives ] ( #alternatives ) instead. **
73
+ * ` cloneElement ` を使うとデータフローの追跡が難しくなるため、代わりに [ 代替手段 ] ( #alternatives ) を試してみてください。
74
74
75
75
---
76
76
77
- ## Usage {/* usage* /}
77
+ ## 使用法 {/* usage* /}
78
78
79
- ### Overriding props of an element {/* overriding-props-of-an-element* /}
79
+ ### 要素の props を上書きする {/* overriding-props-of-an-element* /}
80
80
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 > を指定します。
82
82
83
83
``` js [[1, 5, "<Row title=\\ "Cabbage\\ " />"], [2, 6, "{ isHighlighted: true }"], [3, 4, "clonedElement"]]
84
84
import { cloneElement } from ' react' ;
@@ -90,11 +90,11 @@ const clonedElement = cloneElement(
90
90
);
91
91
```
92
92
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} /> ` になります。
94
94
95
- ** Let's walk through an example to see when it's useful. **
95
+ ** 例を使って、これが役立つ場面を見てみましょう ** 。
96
96
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 として指定します。
98
98
99
99
``` js {6-8}
100
100
export default function List ({ children }) {
@@ -108,7 +108,7 @@ export default function List({ children }) {
108
108
)}
109
109
` ` `
110
110
111
- Let's say the original JSX received by ` List` looks like this:
111
+ 例えば ` List` が受け取る元の JSX が以下のようなものである場合を考えます。
112
112
113
113
` ` ` js {2 - 4 }
114
114
< List>
@@ -118,7 +118,7 @@ Let's say the original JSX received by `List` looks like this:
118
118
< / List>
119
119
` ` `
120
120
121
- By cloning its children, the ` List` can pass extra information to every ` Row` inside. The result looks like this:
121
+ 子要素をクローンすることで、 ` List` は内部のすべての ` Row` に追加情報を渡すことができます。結果は以下のようになります。
122
122
123
123
` ` ` js {4 ,8 ,12 }
124
124
< List>
@@ -137,7 +137,7 @@ By cloning its children, the `List` can pass extra information to every `Row` in
137
137
< / List>
138
138
` ` `
139
139
140
- Notice how pressing "Next" updates the state of the ` List` , and highlights a different row:
140
+ "Next" を押すと ` List` の state が更新され、異なる行がハイライトされることに着目してください。
141
141
142
142
<Sandpack>
143
143
@@ -232,21 +232,21 @@ button {
232
232
233
233
</Sandpack>
234
234
235
- To summarize, the ` List` cloned the ` < Row / > ` elements it received and added an extra prop to them.
235
+ おさらいすると、 ` List` は受け取った ` < Row / > ` 要素をクローンし、それらに追加の props を付加したということです。
236
236
237
237
<Pitfall>
238
238
239
- Cloning children makes it hard to tell how the data flows through your app. Try one of the [alternatives. ](#alternatives)
239
+ 子要素をクローンすると、データがアプリケーションを通じてどのように流れるかを把握するのが難しくなります。[代替手段 ](#alternatives)のいずれかを試してみてください。
240
240
241
241
</Pitfall>
242
242
243
243
---
244
244
245
- ## Alternatives {/*alternatives*/}
245
+ ## 代替手段 {/*alternatives*/}
246
246
247
- ### Passing data with a render prop {/*passing-data-with-a-render-prop*/}
247
+ ### レンダープロップを用いてデータを渡す {/*passing-data-with-a-render-prop*/}
248
248
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` を引数として渡します。
250
250
251
251
` ` ` js {1 ,7 }
252
252
export default function List ({ items, renderItem }) {
@@ -259,7 +259,7 @@ export default function List({ items, renderItem }) {
259
259
})}
260
260
` ` `
261
261
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 ` の実装を渡すことができます。
263
263
264
264
` ` ` js {3 ,7 }
265
265
< List
@@ -274,7 +274,7 @@ The `renderItem` prop is called a "render prop" because it's a prop that specifi
274
274
/ >
275
275
` ` `
276
276
277
- The end result is the same as with ` cloneElement` :
277
+ 最終的な結果は ` cloneElement` と同じです。
278
278
279
279
` ` ` js {4 ,8 ,12 }
280
280
< List>
@@ -293,7 +293,7 @@ The end result is the same as with `cloneElement`:
293
293
< / List>
294
294
` ` `
295
295
296
- However, you can clearly trace where the ` isHighlighted` value is coming from.
296
+ しかし、 ` isHighlighted` 値がどこから来ているかを明確に追跡することができます。
297
297
298
298
<Sandpack>
299
299
@@ -389,22 +389,22 @@ button {
389
389
390
390
</Sandpack>
391
391
392
- This pattern is preferred to ` cloneElement` because it is more explicit.
392
+ このパターンはより明示的であるため、 ` cloneElement` よりも推奨されます。
393
393
394
394
---
395
395
396
- ### Passing data through context {/*passing-data-through-context*/}
396
+ ### コンテクストでデータを渡す {/*passing-data-through-context*/}
397
397
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)ことが可能です。
399
399
400
400
401
- For example, you can call [ ` createContext` ](/reference/react/createContext) to define a ` HighlightContext` :
401
+ 例として、 ` createContext` を呼び出して ` HighlightContext` を定義しましょう。
402
402
403
403
` ` ` js
404
404
export const HighlightContext = createContext (false );
405
405
` ` `
406
406
407
- Your ` List` component can wrap every item it renders into a ` HighlightContext` provider:
407
+ ` List` コンポーネントは、レンダーするすべてのアイテムを ` HighlightContext` プロバイダでラップします。
408
408
409
409
` ` ` js {8 ,10 }
410
410
export default function List ({ items, renderItem }) {
@@ -421,15 +421,15 @@ export default function List({ items, renderItem }) {
421
421
})}
422
422
` ` `
423
423
424
- With this approach, ` Row` does not need to receive an ` isHighlighted` prop at all. Instead, it reads the context:
424
+ このアプローチでは、 ` Row` は props で ` isHighlighted` を受け取る必要が一切ありません。代わりにコンテクストから読み取ります。
425
425
426
426
` ` ` js Row .js {2 }
427
427
export default function Row ({ title }) {
428
428
const isHighlighted = useContext (HighlightContext);
429
429
// ...
430
430
` ` `
431
431
432
- This allows the calling component to not know or worry about passing ` isHighlighted ` to ` < Row > ` :
432
+ これにより、呼び出し元のコンポーネントは ` < Row > ` に ` isHighlighted ` を渡すことについて知る必要も、気にする必要もなくなります。
433
433
434
434
` ` ` js {4 }
435
435
< List
@@ -440,7 +440,7 @@ This allows the calling component to not know or worry about passing `isHighligh
440
440
/ >
441
441
` ` `
442
442
443
- Instead, ` List` and ` Row` coordinate the highlighting logic through context.
443
+ 代わりに、 ` List` と ` Row` はコンテクストを通じ、ハイライトのロジックに関して協調して動作します。
444
444
445
445
<Sandpack>
446
446
@@ -550,13 +550,13 @@ button {
550
550
551
551
</Sandpack>
552
552
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)
554
554
555
555
---
556
556
557
- ### Extracting logic into a custom Hook {/*extracting-logic-into-a-custom-hook*/}
557
+ ### ロジックをカスタムフックに抽出する {/*extracting-logic-into-a-custom-hook*/}
558
558
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` カスタムフックを書くことができます。
560
560
561
561
` ` ` js
562
562
import { useState } from ' react' ;
@@ -575,7 +575,7 @@ export default function useList(items) {
575
575
}
576
576
` ` `
577
577
578
- Then you could use it like this:
578
+ これを以下のように使用できます。
579
579
580
580
` ` ` js {2 ,9 ,13 }
581
581
export default function App () {
@@ -598,7 +598,7 @@ export default function App() {
598
598
}
599
599
` ` `
600
600
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` カスタムフック内にあります。
602
602
603
603
<Sandpack>
604
604
@@ -691,4 +691,4 @@ button {
691
691
692
692
</Sandpack>
693
693
694
- This approach is particularly useful if you want to reuse this logic between different components.
694
+ このアプローチは、特にこのロジックを異なるコンポーネント間で再利用したい場合に有用です。
0 commit comments