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/tutorial-tic-tac-toe.md
+40-40Lines changed: 40 additions & 40 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -921,15 +921,15 @@ Tähän mennessä, sinulla on kaikki peruspalikat ristinolla-peliisi. Saadaksesi
921
921
922
922
### Tilan nostaminen ylös {/*lifting-state-up*/}
923
923
924
-
Currently, each`Square`component maintains a part of the game's state. To check for a winner in a tic-tac-toe game, the `Board`would need to somehow know the state of each of the 9 `Square`components.
924
+
Tällä hetkellä, jokainen`Square`komponentti ylläpitää osaa pelin tilasta. Voittaaksesi ristinolla-pelin, `Board`komponentin täytyy jotenkin tietää jokaisen yhdeksän `Square`komponentin tila.
925
925
926
-
How would you approach that? At first, you might guess that the `Board` needs to "ask" each`Square` for that `Square`'s state. Although this approach is technically possible in React, we discourage it because the code becomes difficult to understand, susceptible to bugs, and hard to refactor. Instead, the best approach is to store the game's state in the parent `Board`component instead of in each `Square`. The `Board`component can tell each`Square`what to display by passing a prop, like you did when you passed a number to each Square.
926
+
Miten lähestyisit tätä? Aluksi, kuten saatat arvata, `Board`:n täytyy "kysyä" jokaiselta`Square`:lta sen tila. Vaikka tämä lähestymistapa on teknisesti mahdollista Reactissa, emme suosittele sitä, koska koodista tulee vaikeaa ymmärtää, altistaen se bugeille, ja vaikea refaktoroida. Sen sijaan, paras lähestymistapa on tallentaa pelin tila ylemmässä `Board`komponentissa jokaisen `Square` komponentin sijaan. `Board`komponentti voi kertoa jokaiselle`Square`komponentille mitä näyttää välittämällä propseja, kuten teit kun välitit numeron jokaiselle `Square` komponentille.
927
927
928
-
**To collect data from multiple children, or to have two child components communicate with each other, declare the shared state in their parent component instead. The parent component can pass that state back down to the children via props. This keeps the child components in sync with each other and with their parent.**
928
+
**Kerätäksesi dataa useammista alakomponenteista, tai saadaksesi kahden alakomponentin kommunikoimaan toistensa kanssa, määritä jaettu tila niitä ylemmässä komponentissa. Ylempi komponentti voi välittää tilan takaisin alakomponenteilleen propseina. Tämä pitää alakomponentit synkronoituina toistensa ja yläkomponentin kanssa.**
929
929
930
-
Lifting state into a parent component is common when React components are refactored.
930
+
Tilan nostaminen yläkomponenttiin on yleistä kun React komponentteja refaktoroidaan.
931
931
932
-
Let's take this opportunity to try it out. Edit the `Board`component so that it declares a state variable named `squares` that defaults to an array of 9 nulls corresponding to the 9 squares:
932
+
Otetaan tilaisuus kokeilla tätä. Muokkaa `Board`komponenttia siten, että se määrittelee tilamuuttujan nimeltään `squares`, joka oletuksena on taulukko, jossa on yhdeksän `null` arvoa vastaten yhdeksää neliötä:
933
933
934
934
```js {3}
935
935
// ...
@@ -941,13 +941,13 @@ export default function Board() {
941
941
}
942
942
```
943
943
944
-
`Array(9).fill(null)`creates an array with nine elements and sets each of them to `null`. The `useState()`call around it declares a `squares`state variable that's initially set to that array. Each entry in the array corresponds to the value of a square. When you fill the board in later, the `squares`array will look like this:
944
+
`Array(9).fill(null)`luo taulukon yhdeksällä kohdalla ja asettaa jokaisen niistä `null` arvoon. `useState()`kutsu sen ympärillä määrittelee `squares`tilamuuttujan, jonka arvo on aluksi asetettu tuohon taulukkoon. Jokainen taulukon kohta vastaa neliön arvoa. Kun täytät pelilaudan myöhemmin, `squares`taulukko näyttää tältä:
945
945
946
946
```jsx
947
947
['O', null, 'X', 'X', 'X', 'O', 'O', null, null]
948
948
```
949
949
950
-
Now your `Board`component needs to pass the `value`prop down to each `Square`that it renders:
950
+
Nyt `Board`komponenttisi täytyy välittää `value`propsi jokaiselle `Square`komponentille, jonka se renderöi:
951
951
952
952
```js {6-8,11-13,16-18}
953
953
exportdefaultfunctionBoard() {
@@ -974,19 +974,19 @@ export default function Board() {
974
974
}
975
975
```
976
976
977
-
Next, you'll edit the `Square`component to receive the `value`prop from the Board component. This will require removing the Square component's own stateful tracking of `value`and the button's `onClick`prop:
977
+
Seuraavakasi, muokkaa `Square`komponentti vastaanottamaan `value`propsi Board komponentilta. Tämä vaatii `Square` komponentin oman tilamuuttujan `value`ja painonapin `onClick`propsin poistamisen:
Each Square will now receive a `value`prop that will either be `'X'`, `'O'`, or`null`for empty squares.
1071
+
Jokainen Square saa nyt `value`propsin, joka on joko `'X'`, `'O'`, tai`null`tyhjille neliöille.
1072
1072
1073
-
Next, you need to change what happens when a `Square` is clicked. The `Board`component now maintains which squares are filled. You'll need to create a way for the `Square` to update the `Board`'s state. Since state is private to a component that defines it, you cannot update the `Board`'s state directly from `Square`.
1073
+
Seuraavaksi, sinun täytyy muuttaa mitä tapahtuu kun `Square`:a klikataan. `Board`komponentti nyt ylläpitää mitkä neliöt ovat täytettyjä. Sinun täytyy luoda tapa `Square`:lle päivittää `Board`:n tila. Koska tila on yksityistä komponentille, joka sen määrittelee, et voi päivittää `Board`:n tilaa suoraan `Square`:sta.
1074
1074
1075
-
Instead, you'll pass down a function from the `Board`component to the `Square`component, and you'll have`Square` call that function when a square is clicked. You'll start with the function that the `Square`component will call when it is clicked. You'll call that function `onSquareClick`:
1075
+
Sen sijaan, välität funktion `Board`komponentista `Square`komponentille, ja kutsut sitä`Square`:sta kun neliötä painetaan. Aloitat funktiosta, jota `Square`komponentti kutsuu kun sitä painetaan. Kutsut sitä `onSquareClick`:ssa:
1076
1076
1077
1077
```js {3}
1078
1078
functionSquare({ value }) {
@@ -1084,7 +1084,7 @@ function Square({ value }) {
1084
1084
}
1085
1085
```
1086
1086
1087
-
Next, you'll add the `onSquareClick`function to the `Square`component's props:
@@ -1096,7 +1096,7 @@ function Square({ value, onSquareClick }) {
1096
1096
}
1097
1097
```
1098
1098
1099
-
Now you'll connect the `onSquareClick`prop to a function in the `Board`component that you'll name`handleClick`. To connect `onSquareClick`to `handleClick` you'll pass a function to the `onSquareClick`prop of the first `Square`component:
1099
+
Nyt yhdistät `onSquareClick`propsin `Board`komponentin funktioon, jonka nimeät`handleClick`. Yhdistääksesi `onSquareClick``handleClick`:iin, välität funktion `onSquareClick`propsin ensimmäiselle `Square`komponentille:
1100
1100
1101
1101
```js {7}
1102
1102
exportdefaultfunctionBoard() {
@@ -1111,7 +1111,7 @@ export default function Board() {
1111
1111
}
1112
1112
```
1113
1113
1114
-
Lastly, you will define the `handleClick`function inside the Board component to update the `squares`array holding your board's state:
@@ -1129,17 +1129,17 @@ export default function Board() {
1129
1129
}
1130
1130
```
1131
1131
1132
-
The `handleClick`function creates a copy of the `squares`array (`nextSquares`) with the JavaScript `slice()`Array method. Then, `handleClick`updates the `nextSquares`array to add `X` to the first (`[0]`index) square.
1132
+
`handleClick`funktio luo kopion `squares`taulukosta (`nextSquares`) JavaScriptin `slice()`taulukkometodilla. Sitten, `handleClick`päivittää `nextSquares`taulukon lisäämällä `X`:n ensimmäiseen (`[0]`indeksi) neliöön.
1133
1133
1134
-
Calling the `setSquares`function lets React know the state of the component has changed. This will trigger a re-render of the components that use the `squares`state (`Board`) as well as its child components (the `Square`components that make up the board).
1134
+
Kutsumalla `setSquares`funktiota kerrot Reactille, että komponentin tila on muuttunut. Tämä käynnistää renderöinnin komponenteille, jotka käyttävät `squares`tilaa (`Board`) sekä sen alakomponenteille (`Square`komponentit, jotka muodostavat pelilaudan).
1135
1135
1136
1136
<Note>
1137
1137
1138
-
JavaScript supports [closures](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures) which means an inner function (e.g.`handleClick`) has access to variables and functions defined in a outer function (e.g.`Board`). The `handleClick`function can read the `squares`state and call the `setSquares`method because they are both defined inside of the `Board`function.
1138
+
JavaScript tukee [closureja](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures), mikä tarkoittaa, että sisäinen funktio (esim.`handleClick`) pääsee käsiksi muuttujiin ja funktioihin, jotka on määritelty ulomman funktion sisällä (esim.`Board`). `handleClick`funktio voi lukea `squares`tilaa ja kutsua `setSquares`metodia, koska ne molemmat on määritelty `Board`funktion sisällä.
1139
1139
1140
1140
</Note>
1141
1141
1142
-
Now you can add X's to the board... but only to the upper left square. Your `handleClick`function is hardcoded to update the index for the upper left square (`0`). Let's update `handleClick`to be able to update any square. Add an argument `i`to the `handleClick`function that takes the index of the square to update:
1142
+
Nyt voit lisätä X:ät pelilaudalle... mutta vain ylävasempaan neliöön. `handleClick`funktiosi on kovakoodattu päivittämään ylävasemman neliön indeksiä (`0`). Päivitetään `handleClick`funktio päivittämään mitä tahansa neliötä. Lisää argumentti `i``handleClick`funktioon, joka ottaa neliön indeksin, jota päivittää:
1143
1143
1144
1144
```js {4,6}
1145
1145
exportdefaultfunctionBoard() {
@@ -1157,27 +1157,27 @@ export default function Board() {
1157
1157
}
1158
1158
```
1159
1159
1160
-
Next, you will need to pass that `i`to `handleClick`. You could try to set the `onSquareClick`prop of square to be `handleClick(0)`directly in the JSX like this, but it won't work:
1160
+
Seuraavaksi, sinun täytyy välittää `i``handleClick`:lle. Voit yrittää asettaa `onSquareClick`propin neliölle suoraan JSX:ssä `handleClick(0)`näin, mutta se ei toimi:
Here is why this doesn't work. The `handleClick(0)`call will be a part of rendering the board component. Because`handleClick(0)`alters the state of the board component by calling `setSquares`, your entire board component will be re-rendered again. But this runs`handleClick(0)`again, leading to an infinite loop:
1166
+
Syy miksi tämä ei toimi on, `handleClick(0)`kustu on osa pelilaudan renderöintiä. Koska`handleClick(0)`muuttaa pelilaudan tilaa kutsumalla `setSquares`:ia, koko pelilauta renderöidään uudelleen. Mutta tämä ajaa`handleClick(0)`uudelleen, mikä johtaa loputtomaan silmukkaan:
1167
1167
1168
1168
<ConsoleBlock level="error">
1169
1169
1170
1170
Too many re-renders. React limits the number of renders to prevent an infinite loop.
1171
1171
1172
1172
</ConsoleBlock>
1173
1173
1174
-
Why didn't this problem happen earlier?
1174
+
Miksi tämä ongelma ei tapahtunut aiemmin?
1175
1175
1176
-
When you were passing `onSquareClick={handleClick}`, you were passing the `handleClick`function down as a prop. You were not calling it! But now you are *calling* that function right away--notice the parentheses in `handleClick(0)`--and that's why it runs too early. You don't *want* to call `handleClick`until the user clicks!
1176
+
Kun välitit `onSquareClick={handleClick}`, välitit `handleClick`funktion propseina. Et kutsunut sitä! Mutta nyt kutsut sitä heti--huomaa sulkeet `handleClick(0)`--ja siksi se ajetaan liian aikaisin. Et *halua* kutsua `handleClick`ennen kuin käyttäjä klikkaa!
1177
1177
1178
-
You could fix this by creating a function like `handleFirstSquareClick` that calls`handleClick(0)`, a function like `handleSecondSquareClick` that calls`handleClick(1)`, and so on. You would pass (rather than call) these functions down as props like `onSquareClick={handleFirstSquareClick}`. This would solve the infinite loop.
1178
+
Voisit korjata tämän tekemällä funktion kuten `handleFirstSquareClick`, joka kutsuu`handleClick(0)`, funktion kuten `handleSecondSquareClick`, joka kutsuu`handleClick(1)`, ja niin edelleen. Välittäisit (et kutsuisi) näitä funktioita propseina kuten `onSquareClick={handleFirstSquareClick}`. Tämä korjaisi loputtoman silmukan.
1179
1179
1180
-
However, defining nine different functions and giving each of them a name is too verbose. Instead, let's do this:
1180
+
Yhdeksän eri funktion määritteleminen ja nimeäminen on liian raskasta. Sen sijaan tehdään näin:
1181
1181
1182
1182
```js {6}
1183
1183
exportdefaultfunctionBoard() {
@@ -1191,9 +1191,9 @@ export default function Board() {
1191
1191
}
1192
1192
```
1193
1193
1194
-
Notice the new `() =>`syntax. Here, `() =>handleClick(0)`is an *arrow function,* which is a shorter way to define functions. When the square is clicked, the code after the `=>` "arrow" will run, calling`handleClick(0)`.
1194
+
Huomaa uusi `() =>`syntaksi. Tässä, `() =>handleClick(0)`on *nuolifunktio*, joka on lyhyempi tapa määritellä funktioita. Kun neliötä painetaan, koodi `=>` "nuolen" jälkeen ajetaan, kutsuen`handleClick(0)`.
1195
1195
1196
-
Now you need to update the other eight squares to call `handleClick`from the arrow functions you pass. Make sure that the argument for each call of the `handleClick`corresponds to the index of the correct square:
1196
+
Nyt sinun tulee päivittää muut kahdeksan neliötä kutsumaan `handleClick`nuolifunktioista, jotka välität. Varmista, että argumentti jokaiselle `handleClick`kutsulle vastaa oikean neliön indeksiä:
1197
1197
1198
1198
```js {6-8,11-13,16-18}
1199
1199
exportdefaultfunctionBoard() {
@@ -1220,13 +1220,13 @@ export default function Board() {
1220
1220
};
1221
1221
```
1222
1222
1223
-
Now you can again add X's to any square on the board by clicking on them:
1223
+
Nyt voit taas lisätä X:ät mihin tahansa neliöön pelilaudalla painamalla niitä:
1224
1224
1225
-

But this time all the state management is handled by the `Board`component!
1227
+
Mutta tällä kertaa tilanhallinta on `Board`komponentin vastuulla!
1228
1228
1229
-
This is what your code should look like:
1229
+
Tämä on mitä koodisi tulisi näyttää:
1230
1230
1231
1231
<Sandpack>
1232
1232
@@ -1319,19 +1319,19 @@ body {
1319
1319
1320
1320
</Sandpack>
1321
1321
1322
-
Now that your state handling is in the `Board`component, the parent `Board`component passes props to the child `Square`components so that they can be displayed correctly. When clicking on a `Square`, the child `Square`component now asks the parent `Board` component to update the state of the board. When the `Board`'s state changes, both the `Board`component and every child `Square`re-renders automatically. Keeping the state of all squares in the `Board`component will allow it to determine the winner in the future.
1322
+
Nyt kun tilanhallintasi on `Board`komponentissa, yläkomponentti `Board`välittää propseja alakomponenteille `Square`komponenteille, jotta ne voidaan näyttää oikein. Kun neliötä painetaan, alakomponentti `Square`kysyy yläkomponentti `Board`:lta tilan päivittämistä pelilaudalla. Kun `Board`:n tila muuttuu, sekä `Board`komponentti että jokainen `Square`renderöidään uudelleen automaattisesti. Pitämällä kaikkien neliöiden tila `Board`komponentissa, se pystyy määrittämään voittajan tulevaisuudessa.
1323
1323
1324
-
Let's recap what happens when a user clicks the top left square on your board to add an `X` to it:
1324
+
Käydään läpi mitä tapahtuu kun käyttäjä painaa ylävasenta neliötä pelilaudalla lisätäkseen siihen `X`:n:
1325
1325
1326
-
1. Clicking on the upper left square runs the function that the `button`received as its `onClick`prop from the `Square`. The `Square`component received that function as its `onSquareClick`prop from the `Board`. The `Board`component defined that function directly in the JSX. It calls`handleClick`with an argument of`0`.
1327
-
1. `handleClick`uses the argument (`0`) to update the first element of the `squares`array from `null`to`X`.
1328
-
1. The `squares`state of the `Board`component was updated, so the `Board`and all of its children re-render. This causes the `value` prop of the `Square` component with index`0`to change from `null`to`X`.
1326
+
1. Ylävasemman neliön klikkaaminen suorittaa funktion, jonka `button`sai `onClick`propsina `Square` komponentilta. `Square`komponentti sai funktion `onSquareClick`propsina `Board` komponentilta. `Board`komponentti määritteli funktion suoraan JSX:ssä. Se kutsuu`handleClick`funktiota argumentilla`0`.
1327
+
1. `handleClick`käyttää argumenttia (`0`) päivittääkseen `squares`taulukon ensimmäisen elementin `null`arvosta`X` arvoon.
1328
+
1. `squares`tila `Board`komponentissa päivitettiin, joten `Board`ja kaikki sen alakomponentit renderöitiin uudelleen. Tämä aiheuttaa `Square` komponentin `value` propin muuttumisen indeksillä`0``null`arvosta`X` arvoon.
1329
1329
1330
-
In the end the user sees that the upper left square has changed from empty to having a `X` after clicking it.
1330
+
Lopussa käyttäjä näkee, että ylävasen neliö on muuttunut tyhjästä `X`:ksi sen painamisen jälkeen.
1331
1331
1332
1332
<Note>
1333
1333
1334
-
The DOM `<button>`element's`onClick`attribute has a special meaning to React because it is a built-in component. For custom components like Square, the naming is up to you. You could give any name to the `Square`'s `onSquareClick`prop or`Board`'s `handleClick`function, and the code would work the same. In React, it's conventional to use`onSomething`names for props which represent events and `handleSomething`for the function definitions which handle those events.
1334
+
DOM `<button>`elementin`onClick`attribuutilla on erityinen merkitys Reactissa, koska se on sisäänrakennettu komponentti. Mukautetuille komponenteille kuten `Square`, nimeäminen on sinusta kiinni. Voit antaa minkä tahansa nimen `Square` komponentin `onSquareClick`propsille tai`Board` komponentin `handleClick`funktiolle, ja koodi toimisi samalla tavalla. Reactissa, yleinen tapa on käyttää`onSomething`nimiä propseille, jotka edustavat tapahtumia ja `handleSomething`funktioille, jotka käsittelevät näitä tapahtumia.
0 commit comments