Skip to content

Commit 2103fec

Browse files
docs: translate cloneElement page (#567)
Co-authored-by: M Haidar Hanif <git@mhaidarhanif.com>
1 parent 7bc26b9 commit 2103fec

File tree

1 file changed

+51
-52
lines changed

1 file changed

+51
-52
lines changed

src/content/reference/react/cloneElement.md

Lines changed: 51 additions & 52 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+
Menggunakan `cloneElement` adalah hal yang jarang terjadi dan dapat menyebabkan kode yang rentan. [Lihat alternatif yang umum.](#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` memungkinkan Anda untuk membuat elemen React baru dengan menggunakan elemen lain sebagai titik awal.
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+
## Referensi {/*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+
Panggil `cloneElement` untuk membuat elemen React berdasarkan `element`, tetapi dengan `props` dan `children` yang berbeda:
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+
[Lihat lebih banyak contoh di bawah ini.](#usage)
4747

48-
#### Parameters {/*parameters*/}
48+
#### Parameter {/*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`: Argumen `element` harus merupakan elemen React yang valid. Misalnya, dapat berupa simpul JSX seperti `<Something />`, hasil dari pemanggilan [`createElement`](/reference/react/createElement), atau hasil dari pemanggilan `cloneElement` lainnya.
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`: Argumen `props` harus berupa objek atau `null`. Jika Anda mengoper `null`, elemen yang di-kloning akan mempertahankan semua `element.props` yang orisinal. Sebaliknya, untuk setiap *prop* di objek `props`, elemen yang dikembalikan akan "memilih" nilai dari `props` daripada nilai dari `element.props`. Sisa *props* lainnya akan diisi dari `element.props` yang orisinal. Jika Anda mengoper `props.key` atau `props.ref`, mereka akan menggantikan yang orisinal.
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+
* **opsional** `...children`: Nol atau lebih simpul anak. Bisa dari simpul React apa pun, termasuk elemen React, *string*, dan *number*. [*portal*](/reference/react-dom/createPortal), simpul kosong (`null`, `undefined`, `true`, dan `false`), dan senarai dari simpul-simpul React. Jika Anda tidak mengoper argumen `...children` apa pun, `element.props.children` yang orisinal akan tetap dipertahankan.
5555

56-
#### Returns {/*returns*/}
56+
#### Kembalian {/*returns*/}
5757

58-
`cloneElement` returns a React element object with a few properties:
58+
`cloneElement` mengembalikan objek elemen React dengan beberapa properti:
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`: Sama seperti `element.type`.
61+
* `props`: Hasil dari penggabungan dangkal antara `element.props` dengan `props` yang Anda oper untuk menimpanya.
62+
* `ref`: `element.ref` yang orisinal, kecuali telah ditimpa oleh `props.ref`.
63+
* `key`: `element.key`, yang orisinal, kecuali telah ditimpa oleh `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+
Biasanya, Anda akan mengembalikan elemen dari sebuah komponen atau membuatnya sebagai anak dari elemen lain. Meskipun Anda mungkin membaca properti elemen tersebut, sebaiknya Anda memperlakukan setiap elemen sebagai objek tersembunyi setelah dibuat, dan hanya me-*render*-nya.
6666

67-
#### Caveats {/*caveats*/}
67+
#### Catatan penting {/*caveats*/}
6868

69-
* Cloning an element **does not modify the original element.**
69+
* Mengkloning sebuah elemen **tidak mengubah elemen yang orisinal**.
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+
* Sebaiknya Anda hanya **mengoper *children* sebagai beberapa argumen ke `cloneElement` jika semuanya diketahui secara statis,** seperti `cloneElement(element, null, child1, child2, child3)`. Jika *children* Anda dinamis, oper seluruh senarai sebagai argumen ketiga: `cloneElement(element, null, listItems)`. Ini memastikan bahwa React akan [memperingatkan Anda tentang `key` yang hilang](/learn/rendering-lists#keeping-list-items-in-order-with-key) untuk setiap *list* dinamis. Untuk *list* statis hal tersebut tidak diperlukan karena tidak pernah diurutkan ulang.
7272

73-
* `cloneElement` makes it harder to trace the data flow, so **try the [alternatives](#alternatives) instead.**
73+
* `cloneElement` membuat pelacakan aliran data lebih sulit, jadi **cobalah beberapa [alternatif](#alternatives) sebagai gantinya.**
7474

7575
---
7676

77-
## Usage {/*usage*/}
77+
## Penggunaan {/*usage*/}
7878

79-
### Overriding props of an element {/*overriding-props-of-an-element*/}
79+
### Menimpa props dari suatu elemen {/*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+
Untuk menimpa *prop* dari beberapa <CodeStep step={1}>elemen React</CodeStep>, oper ke `cloneElement` dengan <CodeStep step={2}>*props* yang ingin Anda timpa</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+
Hasil dari <CodeStep step={3}>elemen yang dikloning</CodeStep> akan menjadi `<Row title="Cabbage" isHighlighted={true} />`.
9494

95-
**Let's walk through an example to see when it's useful.**
95+
**Mari telusuri contoh untuk melihat kapan hal tersebut berguna.**
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+
Bayangkan komponen `List` yang me-*render* [`children`](/learn/passing-props-to-a-component#passing-jsx-as-children)nya sebagai daftar baris yang dapat dipilih dengan tombol "Next" yang dapat merubah baris mana yang dipilih. Komponen `List` perlu me-*render* `Row` yang dipilih secara terpisah, lalu mengkloning setiap anak `<Row>` yang telah diterima, dan menambahkan *prop* `isHighlighted: true` atau `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+
Katakanlah JSX asli yang diterima oleh `List` terlihat seperti ini:
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+
Dengan mengkloning anaknya, `List` dapat meneruskan informasi tambahan ke setiap `Row` di dalamnya. Hasilnya terlihat seperti ini:
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+
Perhatikan saat menekan "Next" akan memperbarui *state* dari `List`, dan menyorot baris yang berbeda:
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+
Ringkasnya, `List` mengkloning elemen `<Row />` yang diterimanya dan menambahkan *prop* tambahan ke dalamnya.
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+
Mengkloning *children* mempersulit untuk mengetahui bagaimana aliran data di aplikasi Anda. Coba salah satu [alternatif.](#alternatives)
240240
241241
</Pitfall>
242242
243243
---
244244
245-
## Alternatives {/*alternatives*/}
245+
## Alternatif {/*alternatives*/}
246246
247-
### Passing data with a render prop {/*passing-data-with-a-render-prop*/}
247+
### Mengoper data dengan render prop {/*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+
Daripada menggunakan `cloneElement`, pertimbangkan untuk menerima *render prop* seperti `renderItem`. Di sini, `List` menerima `renderItem` sebagai *prop*. `List` memanggil `renderItem` untuk setiap item dan mengoper `isHighlighted` sebagai argumen:
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+
*Prop* `renderItem` disebut "render prop" karena merupakan *prop* yang menentukan cara me-*render* sesuatu. Misalnya, Anda dapat mengoper `renderItem` yang me-*render* `<Row>` dengan nilai `isHighlighted` yang diberikan:
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+
Hasil akhirnya sama dengan `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+
Namun, Anda dapat dengan mudah melacak dari mana nilai `isHighlighted` berasal.
297297
298298
<Sandpack>
299299
@@ -389,22 +389,21 @@ button {
389389
390390
</Sandpack>
391391
392-
This pattern is preferred to `cloneElement` because it is more explicit.
392+
Pola ini lebih anjurkan daripada `cloneElement` karena lebih eksplisit.
393393
394394
---
395395
396-
### Passing data through context {/*passing-data-through-context*/}
396+
### Mengoper data melalui context {/*passing-data-through-context*/}
397397
398-
Another alternative to `cloneElement` is to [pass data through context.](/learn/passing-data-deeply-with-context)
398+
Alternatif lain untuk `cloneElement` adalah [mengoper data melalui *context*.](/learn/passing-data-deeply-with-context)
399399
400-
401-
For example, you can call [`createContext`](/reference/react/createContext) to define a `HighlightContext`:
400+
Sebagai contoh, Anda dapat memanggil [`createContext`](/reference/react/createContext) untuk mendefinisikan `HighlightContext`:
402401
403402
```js
404403
export const HighlightContext = createContext(false);
405404
```
406405
407-
Your `List` component can wrap every item it renders into a `HighlightContext` provider:
406+
Komponen `List` dapat menggabungkan setiap item yang di-*render* ke dalam *provider* `HighlightContext`:
408407
409408
```js {8,10}
410409
export default function List({ items, renderItem }) {
@@ -421,15 +420,15 @@ export default function List({ items, renderItem }) {
421420
})}
422421
```
423422
424-
With this approach, `Row` does not need to receive an `isHighlighted` prop at all. Instead, it reads the context:
423+
Dengan pendekatan ini, `Row` tidak perlu menerima *prop* `isHighlighted` sama sekali. Sebaliknya, dengan membaca *context*-nya:
425424
426425
```js Row.js {2}
427426
export default function Row({ title }) {
428427
const isHighlighted = useContext(HighlightContext);
429428
// ...
430429
```
431430
432-
This allows the calling component to not know or worry about passing `isHighlighted` to `<Row>`:
431+
Hal ini memungkinkan komponen pemanggil untuk tidak mengetahui atau peduli tentang pengoperan `isHighlighted` ke `<Row>`:
433432
434433
```js {4}
435434
<List
@@ -440,7 +439,7 @@ This allows the calling component to not know or worry about passing `isHighligh
440439
/>
441440
```
442441
443-
Instead, `List` and `Row` coordinate the highlighting logic through context.
442+
Sebagai gantinya, `List` dan `Row` mengoordinasikan logika penyorotan melalui *context*.
444443
445444
<Sandpack>
446445
@@ -550,13 +549,13 @@ button {
550549
551550
</Sandpack>
552551
553-
[Learn more about passing data through context.](/reference/react/useContext#passing-data-deeply-into-the-tree)
552+
[Pelajari lebih lanjut tentang mengoper data melalui *context*.](/reference/react/useContext#passing-data-deeply-into-the-tree)
554553
555554
---
556555
557-
### Extracting logic into a custom Hook {/*extracting-logic-into-a-custom-hook*/}
556+
### Mengekstraksi logika ke dalam Hook kustom {/*extracting-logic-into-a-custom-hook*/}
558557
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:
558+
Pendekatan lain yang dapat Anda coba adalah mengekstrak logika "non-visual" ke dalam Hook Anda sendiri, dan menggunakan informasi yang dikembalikan oleh Hook Anda untuk memutuskan apa yang akan di-*render*. Misalnya, Anda dapat menulis Hook kustom `useList` seperti ini:
560559
561560
```js
562561
import { useState } from 'react';
@@ -575,7 +574,7 @@ export default function useList(items) {
575574
}
576575
```
577576
578-
Then you could use it like this:
577+
Lalu Anda dapat menggunakannya seperti ini:
579578
580579
```js {2,9,13}
581580
export default function App() {
@@ -598,7 +597,7 @@ export default function App() {
598597
}
599598
```
600599
601-
The data flow is explicit, but the state is inside the `useList` custom Hook that you can use from any component:
600+
Aliran datanya eksplisit, tetapi *state* ada di dalam Hook kustom `useList` yang dapat Anda gunakan dari komponen apa pun:
602601
603602
<Sandpack>
604603
@@ -691,4 +690,4 @@ button {
691690
692691
</Sandpack>
693692
694-
This approach is particularly useful if you want to reuse this logic between different components.
693+
Pendekatan ini sangat berguna jika Anda ingin menggunakan kembali logika ini di komponen yang berbeda.

0 commit comments

Comments
 (0)