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/keeping-components-pure.md
+9-9Lines changed: 9 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -85,19 +85,19 @@ Anda bisa menganggap komponen Anda sebagai resep: jika Anda mengikuti resep ters
85
85
86
86
<Illustrationsrc="/images/docs/illustrations/i_puritea-recipe.png"alt="Sebuah resep teh untuk x orang: membutuhkan x gelas air, tambahkan teh sebanyak x sendok, tambahkan rempah sebanyak 0,5 sendok, dan 0,5 gelas susu" />
87
87
88
-
## Side Effects: (un)intended consequences {/*side-effects-unintended-consequences*/}
88
+
## Efek Samping: Konsekuensi yang (tidak) diinginkan {/*side-effects-unintended-consequences*/}
89
89
90
-
React's rendering process must always be pure. Components should only *return* their JSX, and not *change* any objects or variables that existed before rendering—that would make them impure!
90
+
Proses *render* React harus selalu murni. Komponen hanya *mengembalikan*JSX mereka dan tidak mengubah objek atau variabel apapun yang telah ada sebelumnya--ini membuat komponen tersebut menjadi tidak murni!
91
91
92
-
Here is a component that breaks this rule:
92
+
Ini contoh komponen yang tidak mengikuti aturan tersebut:
93
93
94
94
<Sandpack>
95
95
96
96
```js
97
97
let guest =0;
98
98
99
99
functionCup() {
100
-
//Bad: changing a preexisting variable!
100
+
//Buruk: mengubah variabel yang sudah ada!
101
101
guest = guest +1;
102
102
return<h2>Tea cup for guest #{guest}</h2>;
103
103
}
@@ -115,11 +115,11 @@ export default function TeaSet() {
115
115
116
116
</Sandpack>
117
117
118
-
This component is reading and writing a`guest`variable declared outside of it. This means that **calling this component multiple times will produce different JSX!**And what's more, if _other_ components read `guest`, they will produce different JSX, too, depending on when they were rendered! That's not predictable.
118
+
Komponen ini membaca dan menulis sebuah variabel`guest`yang telah dideklarasi di luar komponen tersebut. Ini berarti **memanggil komponen JSX ini berkali-kali akan menghasilkan JSX yang berbeda pada setiap percobaan!**Bukan hanya itu, jika ada komponen **lain* yang juga membaca `guest`, komponen tersebut juga akan menghasilkan JSX yang berbeda, bergantung kepada kapan dia di-*render*. Hal ini sangat sulit untuk diprediksi.
119
119
120
-
Going back to our formula <Math><MathI>y</MathI> = 2<MathI>x</MathI></Math>, now even if <Math><MathI>x</MathI> = 2</Math>, we cannot trust that <Math><MathI>y</MathI> = 4</Math>. Our tests could fail, our users would be baffled, planes would fall out of the sky—you can see how this would lead to confusing bugs!
120
+
Kembali ke rumus <Math><MathI>y</MathI> = 2<MathI>x</MathI></Math>, meskipun <Math><MathI>x</MathI> = 2</Math>, kita tidak bisa menjamin <Math><MathI>y</MathI> = 4</Math>. Kasus uji kita akan gagal, pengguna kita menjadi sangat bingung, dan pesawat akan berjatuhan dari langit--Anda bisa melihat bagaimana ini akan berujung kepada *bug* yang sangat membingungkan!
121
121
122
-
You can fix this component by [passing`guest`as a prop instead](/learn/passing-props-to-a-component):
122
+
Anda bisa memperbaiki komponen ini dengan [memberikan`guest`sebagai sebuah *prop*](/learn/passing-props-to-a-component):
123
123
124
124
<Sandpack>
125
125
@@ -141,9 +141,9 @@ export default function TeaSet() {
141
141
142
142
</Sandpack>
143
143
144
-
Now your component is pure, as the JSX it returns only depends on the`guest` prop.
144
+
Sekarang, komponen Anda menjadi murni karena JSX yang dikembalikan hanya bergantung kepada *prop*`guest`.
145
145
146
-
In general, you should not expect your components to be rendered in any particular order. It doesn't matter if you call <Math><MathI>y</MathI> = 2<MathI>x</MathI></Math> before or after <Math><MathI>y</MathI> = 5<MathI>x</MathI></Math>: both formulas will resolve independently of each other. In the same way, each component should only "think for itself", and not attempt to coordinate with or depend upon others during rendering. Rendering is like a school exam: each component should calculate JSX on their own!
146
+
Secara umum, Anda jangan mengharapkan komponen Anda untuk di-*render* mengikuti suatu urutan yang pasti. Meskipun Anda memanggil <Math><MathI>y</MathI> = 2<MathI>x</MathI></Math> sebelum atau sesudah <Math><MathI>y</MathI> = 5<MathI>x</MathI></Math>, kedua rumus tersebut akan berjalan secara independen. Oleh karena itu, setiap komponen sebaiknya hanya "mengurus dirinya sendiri" dan tidak mencoba untuk berkoordinasi atau bergantung kepada komponen lain selama proses *render* berjalan. Proses *render* mirip dengan ujian di sekolah, setiap komponen harus mengalkulasi JSX dia sendiri.
0 commit comments