You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/learn/sharing-state-between-components.md
+47-40Lines changed: 47 additions & 40 deletions
Original file line number
Diff line number
Diff line change
@@ -11,22 +11,24 @@ Terkadang, Anda ingin 2 komponen selalu berubah secara bersamaan. Untuk melakuka
11
11
12
12
<YouWillLearn>
13
13
14
-
-How to share state between components by lifting it up
15
-
-What are controlled and uncontrolled components
14
+
-Bagaimana cara berbagi state antar komponen dengan *lifting state up*
15
+
-Apa itu komponen terkendali dan tidak terkendali
16
16
17
17
</YouWillLearn>
18
18
19
-
## Lifting state up by example {/*lifting-state-up-by-example*/}
19
+
## Contoh Lifting State Up {/*lifting-state-up-by-example*/}
20
20
21
-
In this example, a parent `Accordion` component renders two separate `Panel`s:
21
+
22
+
Pada contoh ini, komponen induk `Accordion` merender dua komponen `Panel` terpisah:
22
23
23
24
*`Accordion`
24
25
-`Panel`
25
26
-`Panel`
26
27
27
-
Each `Panel` component has a boolean `isActive` state that determines whether its content is visible.
28
+
Setiap komponen `Panel` memiliki *state* boolean `isActive` yang menentukan apakah kontennya terlihat.
29
+
28
30
29
-
Press the Show button for both panels:
31
+
Tekan tombol Tampilkan untuk kedua panel:
30
32
31
33
<Sandpack>
32
34
@@ -42,7 +44,7 @@ function Panel({ title, children }) {
42
44
<p>{children}</p>
43
45
) : (
44
46
<button onClick={() =>setIsActive(true)}>
45
-
Show
47
+
Tampilkan
46
48
</button>
47
49
)}
48
50
</section>
@@ -53,11 +55,11 @@ export default function Accordion() {
53
55
return (
54
56
<>
55
57
<h2>Almaty, Kazakhstan</h2>
56
-
<Panel title="About">
57
-
With a population of about 2 million, Almaty is Kazakhstan's largest city. From 1929 to 1997, it was its capital city.
58
+
<Panel title="Tentang">
59
+
Dengan populasi sekitar 2 juta, Almaty adalah kota terbesar di Kazakhstan. Dari tahun 1929hingga1997, itu adalah ibu kota negara tersebut.
58
60
</Panel>
59
-
<Panel title="Etymology">
60
-
The name comes from <span lang="kk-KZ">алма</span>, 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 <i lang="la">Malus sieversii</i> is considered a likely candidate for the ancestor of the modern domestic apple.
61
+
<Panel title="Etimologi">
62
+
Nama berasal dari <span lang="kk-KZ">алма</span>, kata Kazakh untuk "apel" dan sering diterjemahkan sebagai "penuh dengan apel". Faktanya, wilayah sekitar Almaty dipercaya sebagai rumah leluhur apel, dan <i lang="la">Malus sieversii</i>si liar dianggap sebagai kandidat yang mungkin untuk leluhur apel domestik modern.
61
63
</Panel>
62
64
</>
63
65
);
@@ -74,59 +76,62 @@ h3, p { margin: 5px 0px; }
74
76
75
77
</Sandpack>
76
78
77
-
Notice how pressing one panel's button does not affect the other panel--they are independent.
79
+
Perhatikan bagaimana menekan tombol satu panel tidak memengaruhi panel lainnya - mereka independen.
78
80
79
81
<DiagramGroup>
80
82
81
83
<Diagramname="sharing_state_child"height={367}width={477}alt="Diagram showing a tree of three components, one parent labeled Accordion and two children labeled Panel. Both Panel components contain isActive with value false.">
82
84
83
-
Initially, each`Panel`'s `isActive` state is `false`, so they both appear collapsed
85
+
Awalnya, setiap`Panel` memiliki state `isActive`dengan nilai`false`, sehingga keduanya terlihat tertutup
84
86
85
87
</Diagram>
86
88
87
89
<Diagramname="sharing_state_child_clicked"height={367}width={480}alt="The same diagram as the previous, with the isActive of the first child Panel component highlighted indicating a click with the isActive value set to true. The second Panel component still contains value false." >
88
90
89
-
Clicking either `Panel`'s button will only update that `Panel`'s `isActive` state alone
91
+
Menekan tombol`Panel` mana pun hanya akan memperbarui state `isActive` dari `Panel` itu sendiri
90
92
91
93
</Diagram>
92
94
93
95
</DiagramGroup>
94
96
95
-
**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?
97
+
**Tetapi sekarang katakanlah Anda ingin mengubah panel tersebut sehingga hanya satu panel yang dibuka pada satu waktu.** Dengan desain diatas, membuka panel kedua harus menutup panel pertama. Bagaimana Anda melakukannya?
98
+
99
+
Untuk mengkoordinasikan kedua panel ini, Anda perlu "mengangkat *state* mereka" ke komponen induk dalam tiga langkah:
96
100
97
-
To coordinate these two panels, you need to "lift their state up" to a parent component in three steps:
101
+
1.**Hapus***state* dari komponen anak.
102
+
2.**Oper** data dari komponen induk.
103
+
3.**Tambahkan***state* ke komponen induk dan oper bersamaan dengan *Event Handlers*.
98
104
99
-
1.**Remove** state from the child components.
100
-
2.**Pass** hardcoded data from the common parent.
101
-
3.**Add** state to the common parent and pass it down together with the event handlers.
105
+
Cara ini akan memungkinkan komponen `Accordion` untuk mengkoordinasikan kedua `Panel` dan hanya membuka satu panel pada satu waktu.
102
106
103
-
This will allow the `Accordion` component to coordinate both `Panel`s and only expand one at a time.
107
+
### Langkah 1: Hapus state dari komponen anak {/*step-1-remove-state-from-the-child-components*/}
104
108
105
-
### Step 1: Remove state from the child components {/*step-1-remove-state-from-the-child-components*/}
106
109
107
-
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:
110
+
Anda akan memberikan kontrol `isActive` dari`Panel` ke komponen induknya. Ini berarti komponen induk akan mengoper `isActive`ke`Panel`sebagai *prop*. Mulai dengan**menghapus baris ini**dari komponen`Panel`:
108
111
109
112
```js
110
113
const [isActive, setIsActive] =useState(false);
111
114
```
112
115
113
-
And instead, add `isActive` to the `Panel`'s list of props:
116
+
Lalu, tambahkan`isActive`ke daftar *prop*`Panel`:
114
117
115
118
```js
116
-
function Panel({ title, children, isActive }) {
119
+
functionPanel({ title, children, isActive })
117
120
```
118
121
119
-
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!
122
+
Sekarang komponen induk `Panel` dapat *mengontrol* `isActive` dengan [mengoper sebagai prop.](/learn/passing-props-to-a-component) Sebaliknya, komponen `Panel` sekarang tidak memiliki *kontrol* atas nilai `isActive` - sekarang terserah komponen induk!
120
123
121
-
### Step 2: Pass hardcoded data from the common parent {/*step-2-pass-hardcoded-data-from-the-common-parent*/}
122
124
123
-
To lift state up, you must locate the closest common parent component of*both*ofthe child components that you want to coordinate:
125
+
### Langkah 2: Oper data yang diperlukan dari komponen induk {/*step-2-pass-hardcoded-data-from-the-common-parent*/}
124
126
125
-
*`Accordion`*(closest common parent)*
127
+
Untuk mengangkat *state*, Anda harus menemukan komponen induk yang paling dekat dari *kedua* komponen anak yang ingin Anda koordinasikan:
128
+
129
+
*`Accordion`*(Komponen induk terdekat)*
126
130
-`Panel`
127
131
-`Panel`
128
132
129
-
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:
133
+
Pada contoh ini, komponen `Accordion` adalah yang terdekat. Karena komponen ini berada di atas kedua panel dan dapat mengontrol *prop* mereka, komponen ini akan menjadi "sumber kebenaran" untuk panel mana yang sedang aktif. Buat komponen `Accordion` mengoper nilai `isActive` yang telah ditentukan sebelumnya (misalnya, `true`) ke kedua panel:
134
+
130
135
131
136
<Sandpack>
132
137
@@ -137,11 +142,11 @@ export default function Accordion() {
137
142
return (
138
143
<>
139
144
<h2>Almaty, Kazakhstan</h2>
140
-
<Panel title="About" isActive={true}>
141
-
With a population of about 2 million, Almaty is Kazakhstan's largest city. From 1929 to 1997, it was its capital city.
145
+
<Panel title="Tentang" isActive={true}>
146
+
Dengan populasi sekitar 2 juta, Almaty adalah kota terbesar di Kazakhstan. Dari tahun 1929 hingga 1997, itu adalah ibu kota negara tersebut.
142
147
</Panel>
143
-
<Panel title="Etymology" isActive={true}>
144
-
The name comes from <span lang="kk-KZ">алма</span>, 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 <i lang="la">Malus sieversii</i> is considered a likely candidate for the ancestor of the modern domestic apple.
148
+
<Panel title="Etimologi" isActive={true}>
149
+
Nama berasal dari <span lang="kk-KZ">алма</span>, kata Kazakh untuk "apel" dan sering diterjemahkan sebagai "penuh dengan apel". Faktanya, wilayah sekitar Almaty dipercaya sebagai rumah leluhur apel, dan <i lang="la">Malus sieversii</i> si liar dianggap sebagai kandidat yang mungkin untuk leluhur apel domestik modern.
Try editing the hardcoded `isActive` values in the `Accordion` component and see the result on the screen.
181
+
Coba edit nilai `isActive` yang telah ditentukan sebelumnya di komponen `Accordion` dan lihat hasilnya di layar.
182
+
183
+
### Langkah 3: Tambahkan state ke komponen induk {/*step-3-add-state-to-the-common-parent*/}
184
+
185
+
Memindahkan *state* keatas sering mengubah sifat apa yang Anda simpan sebagai *state*.
177
186
178
-
### Step 3: Add state to the common parent {/*step-3-add-state-to-the-common-parent*/}
179
187
180
-
Lifting state up often changes the nature of what you're storing as state.
181
188
182
-
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 a number as the index of the active `Panel` for the state variable:
189
+
Dalam contoh ini, Anda ingin mengubah `Accordion` sehingga hanya satu panel yang dapat dibuka pada satu waktu. Ini berarti bahwa komponen induk `Accordion` perlu melacak *panel mana* yang sedang aktif. Alih-alih nilai `boolean`, ini dapat menggunakan angka sebagai indeks `Panel`aktif untuk variabel *state*:
When the `activeIndex` is `0`, the first panel is active, and when it's`1`, it's the second one.
195
+
Ketika `activeIndex`bernilai`0`, panel pertama aktif, dan ketika bernilai`1`, panel kedua aktif.
189
196
190
-
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):
197
+
Menekan tombol "Tampilkan" di salah satu`Panel`perlu mengubah indeks aktif di `Accordion`. Sebuah`Panel`tidak dapat mengatur *state*`activeIndex`secara langsung karena didefinisikan di dalam `Accordion`. Komponen`Accordion`perlu *mengeksplisitkan* komponen`Panel`untuk mengubah *state*-nya dengan [mengoper event handler sebagai prop](/learn/responding-to-events#passing-event-handlers-as-props):
0 commit comments