Skip to content

Commit e2f46eb

Browse files
committed
until step 3
1 parent bb4556b commit e2f46eb

File tree

1 file changed

+47
-40
lines changed

1 file changed

+47
-40
lines changed

src/content/learn/sharing-state-between-components.md

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,24 @@ Terkadang, Anda ingin 2 komponen selalu berubah secara bersamaan. Untuk melakuka
1111

1212
<YouWillLearn>
1313

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
1616

1717
</YouWillLearn>
1818

19-
## Lifting state up by example {/*lifting-state-up-by-example*/}
19+
## Contoh Lifting State Up {/*lifting-state-up-by-example*/}
2020

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:
2223

2324
* `Accordion`
2425
- `Panel`
2526
- `Panel`
2627

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+
2830

29-
Press the Show button for both panels:
31+
Tekan tombol Tampilkan untuk kedua panel:
3032

3133
<Sandpack>
3234

@@ -42,7 +44,7 @@ function Panel({ title, children }) {
4244
<p>{children}</p>
4345
) : (
4446
<button onClick={() => setIsActive(true)}>
45-
Show
47+
Tampilkan
4648
</button>
4749
)}
4850
</section>
@@ -53,11 +55,11 @@ export default function Accordion() {
5355
return (
5456
<>
5557
<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 1929 hingga 1997, itu adalah ibu kota negara tersebut.
5860
</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.
6163
</Panel>
6264
</>
6365
);
@@ -74,59 +76,62 @@ h3, p { margin: 5px 0px; }
7476

7577
</Sandpack>
7678

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.
7880

7981
<DiagramGroup>
8082

8183
<Diagram name="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.">
8284

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
8486

8587
</Diagram>
8688

8789
<Diagram name="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." >
8890

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
9092

9193
</Diagram>
9294

9395
</DiagramGroup>
9496

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:
96100

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*.
98104

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.
102106

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*/}
104108

105-
### Step 1: Remove state from the child components {/*step-1-remove-state-from-the-child-components*/}
106109

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`:
108111

109112
```js
110113
const [isActive, setIsActive] = useState(false);
111114
```
112115

113-
And instead, add `isActive` to the `Panel`'s list of props:
116+
Lalu, tambahkan `isActive` ke daftar *prop* `Panel`:
114117

115118
```js
116-
function Panel({ title, children, isActive }) {
119+
function Panel({ title, children, isActive })
117120
```
118121

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!
120123

121-
### Step 2: Pass hardcoded data from the common parent {/*step-2-pass-hardcoded-data-from-the-common-parent*/}
122124

123-
To lift state up, you must locate the closest common parent component of *both* of the 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*/}
124126

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)*
126130
- `Panel`
127131
- `Panel`
128132

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+
130135

131136
<Sandpack>
132137

@@ -137,11 +142,11 @@ export default function Accordion() {
137142
return (
138143
<>
139144
<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.
142147
</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.
145150
</Panel>
146151
</>
147152
);
@@ -155,7 +160,7 @@ function Panel({ title, children, isActive }) {
155160
<p>{children}</p>
156161
) : (
157162
<button onClick={() => setIsActive(true)}>
158-
Show
163+
Tampilkan
159164
</button>
160165
)}
161166
</section>
@@ -173,21 +178,23 @@ h3, p { margin: 5px 0px; }
173178

174179
</Sandpack>
175180

176-
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*.
177186

178-
### Step 3: Add state to the common parent {/*step-3-add-state-to-the-common-parent*/}
179187

180-
Lifting state up often changes the nature of what you're storing as state.
181188

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*:
183190

184191
```js
185192
const [activeIndex, setActiveIndex] = useState(0);
186193
```
187194

188-
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.
189196

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):
191198

192199
```js
193200
<>

0 commit comments

Comments
 (0)