Skip to content

Commit dda200c

Browse files
committed
Rename handleclick props to onClick
1 parent a8ab71e commit dda200c

File tree

4 files changed

+50
-52
lines changed

4 files changed

+50
-52
lines changed

src/content/1/en/part1d.md

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -428,8 +428,8 @@ const History = (props) => {
428428
}
429429

430430
// highlight-start
431-
const Button = ({ handleClick, text }) => (
432-
<button onClick={handleClick}>
431+
const Button = ({ onClick, text }) => (
432+
<button onClick={onClick}>
433433
{text}
434434
</button>
435435
)
@@ -454,8 +454,8 @@ const App = () => {
454454
<div>
455455
{left}
456456
// highlight-start
457-
<Button handleClick={handleLeftClick} text='left' />
458-
<Button handleClick={handleRightClick} text='right' />
457+
<Button onClick={handleLeftClick} text='left' />
458+
<Button onClick={handleRightClick} text='right' />
459459
// highlight-end
460460
{right}
461461
<History allClicks={allClicks} />
@@ -497,8 +497,8 @@ don't write more code but rather find and fix the problem **immediately**. There
497497
Old-school, print-based debugging is always a good idea. If the component
498498

499499
```js
500-
const Button = ({ handleClick, text }) => (
501-
<button onClick={handleClick}>
500+
const Button = ({ onClick, text }) => (
501+
<button onClick={onClick}>
502502
{text}
503503
</button>
504504
)
@@ -509,9 +509,9 @@ is not working as intended, it's useful to start printing its variables out to t
509509
```js
510510
const Button = (props) => {
511511
console.log(props) // highlight-line
512-
const { handleClick, text } = props
512+
const { onClick, text } = props
513513
return (
514-
<button onClick={handleClick}>
514+
<button onClick={onClick}>
515515
{text}
516516
</button>
517517
)
@@ -1030,31 +1030,31 @@ Let's extract the button into its own component:
10301030

10311031
```js
10321032
const Button = (props) => (
1033-
<button onClick={props.handleClick}>
1033+
<button onClick={props.onClick}>
10341034
{props.text}
10351035
</button>
10361036
)
10371037
```
10381038

1039-
The component gets the event handler function from the _handleClick_ prop, and the text of the button from the _text_ prop. Lets use the new component:
1039+
The component gets the event handler function from the _onClick_ prop, and the text of the button from the _text_ prop. Lets use the new component:
10401040

10411041
```js
10421042
const App = (props) => {
10431043
// ...
10441044
return (
10451045
<div>
10461046
{value}
1047-
<Button handleClick={() => setToValue(1000)} text="thousand" /> // highlight-line
1048-
<Button handleClick={() => setToValue(0)} text="reset" /> // highlight-line
1049-
<Button handleClick={() => setToValue(value + 1)} text="increment" /> // highlight-line
1047+
<Button onClick={() => setToValue(1000)} text="thousand" /> // highlight-line
1048+
<Button onClick={() => setToValue(0)} text="reset" /> // highlight-line
1049+
<Button onClick={() => setToValue(value + 1)} text="increment" /> // highlight-line
10501050
</div>
10511051
)
10521052
}
10531053
```
10541054

10551055
Using the <i>Button</i> component is simple, although we have to make sure that we use the correct attribute names when passing props to the component.
10561056

1057-
![using correct attribute names code screenshot](../../images/1/12e.png)
1057+
![using correct attribute names code screenshot](../../images/1/12f.png)
10581058

10591059
### Do Not Define Components Within Components
10601060

@@ -1065,7 +1065,7 @@ We will change the application by defining a new component inside of the <i>App<
10651065
```js
10661066
// This is the right place to define a component
10671067
const Button = (props) => (
1068-
<button onClick={props.handleClick}>
1068+
<button onClick={props.onClick}>
10691069
{props.text}
10701070
</button>
10711071
)
@@ -1084,9 +1084,9 @@ const App = () => {
10841084
return (
10851085
<div>
10861086
<Display value={value} /> // highlight-line
1087-
<Button handleClick={() => setToValue(1000)} text="thousand" />
1088-
<Button handleClick={() => setToValue(0)} text="reset" />
1089-
<Button handleClick={() => setToValue(value + 1)} text="increment" />
1087+
<Button onClick={() => setToValue(1000)} text="thousand" />
1088+
<Button onClick={() => setToValue(0)} text="reset" />
1089+
<Button onClick={() => setToValue(value + 1)} text="increment" />
10901090
</div>
10911091
)
10921092
}
@@ -1100,7 +1100,7 @@ Let's instead move the <i>Display</i> component function to its correct place, w
11001100
const Display = props => <div>{props.value}</div>
11011101

11021102
const Button = (props) => (
1103-
<button onClick={props.handleClick}>
1103+
<button onClick={props.onClick}>
11041104
{props.text}
11051105
</button>
11061106
)
@@ -1116,9 +1116,9 @@ const App = () => {
11161116
return (
11171117
<div>
11181118
<Display value={value} />
1119-
<Button handleClick={() => setToValue(1000)} text="thousand" />
1120-
<Button handleClick={() => setToValue(0)} text="reset" />
1121-
<Button handleClick={() => setToValue(value + 1)} text="increment" />
1119+
<Button onClick={() => setToValue(1000)} text="thousand" />
1120+
<Button onClick={() => setToValue(0)} text="reset" />
1121+
<Button onClick={() => setToValue(value + 1)} text="increment" />
11221122
</div>
11231123
)
11241124
}

src/content/1/fi/osa1c.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ const App = () => {
621621
622622
Koska meillä on nyt uudelleenkäytettävä komponentti <i>Button</i>, sovellukselle on lisätty uutena toiminnallisuutena nappi, jolla laskurin arvoa voi vähentää.
623623
624-
Tapahtumankäsittelijä välitetään napeille propsin _onClick_ välityksellä. Omia komponentteja luotaessa propsin nimen voi periaatteessa valita täysin vapaasti, mutta esim. Reactin [tutoriaali](https://react.dev/learn/responding-to-events#naming-event-handler-props) suosittelee, että tapahtumankäsittelijän sisältävän propsin nimi alkaa etuliitteellä _on_ ja jatkuu isolla kirjaimella.
624+
Tapahtumankäsittelijä välitetään napeille propsin _onClick_ välityksellä. Omia komponentteja luotaessa propsin nimen voi periaatteessa valita täysin vapaasti, mutta esim. Reactin [tutoriaali](https://react.dev/learn/responding-to-events#naming-event-handler-props) suosittelee, että tapahtumankäsittelijän sisältävän propsin nimi alkaa etuliitteellä _on_ ja jatkuu isolla kirjaimella eli on muotoa _onSomething_.
625625
626626
### Tilan muutos aiheuttaa uudelleenrenderöitymisen
627627

src/content/1/fi/osa1d.md

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -422,8 +422,8 @@ const History = (props) => {
422422
}
423423

424424
// highlight-start
425-
const Button = ({ handleClick, text }) => (
426-
<button onClick={handleClick}>
425+
const Button = ({ onClick, text }) => (
426+
<button onClick={onClick}>
427427
{text}
428428
</button>
429429
)
@@ -446,15 +446,13 @@ const App = () => {
446446

447447
return (
448448
<div>
449-
<div>
450-
{left}
451-
// highlight-start
452-
<Button handleClick={handleLeftClick} text='left' />
453-
<Button handleClick={handleRightClick} text='right' />
454-
// highlight-end
455-
{right}
456-
<History allClicks={allClicks} />
457-
</div>
449+
{left}
450+
// highlight-start
451+
<Button onClick={handleLeftClick} text='left' />
452+
<Button onClick={handleRightClick} text='right' />
453+
// highlight-end
454+
{right}
455+
<History allClicks={allClicks} />
458456
</div>
459457
)
460458
}
@@ -493,8 +491,8 @@ Jos ja kun koodi ei käänny eli selaimessa alkaa näkyä punaista
493491
Vanha kunnon printtaukseen perustuva debuggaus on monesti toimiva tapa. Eli jos esim. komponentissa
494492

495493
```js
496-
const Button = ({ handleClick, text }) => (
497-
<button onClick={handleClick}>
494+
const Button = ({ onClick, text }) => (
495+
<button onClick={onClick}>
498496
{text}
499497
</button>
500498
)
@@ -505,9 +503,9 @@ olisi ongelma, kannattaa komponentista alkaa printtailla konsoliin. Pystyäksemm
505503
```js
506504
const Button = (props) => {
507505
console.log(props) // highlight-line
508-
const { handleClick, text } = props
506+
const { onClick, text } = props
509507
return (
510-
<button onClick={handleClick}>
508+
<button onClick={onClick}>
511509
{text}
512510
</button>
513511
)
@@ -1022,31 +1020,31 @@ Eriytetään vielä painike omaksi komponentikseen:
10221020

10231021
```js
10241022
const Button = (props) => (
1025-
<button onClick={props.handleClick}>
1023+
<button onClick={props.onClick}>
10261024
{props.text}
10271025
</button>
10281026
)
10291027
```
10301028

1031-
Komponentti saa siis propsina _handleClick_ tapahtumankäsittelijän ja propsina _text_ merkkijonon, jonka se renderöi painikkeen tekstiksi. Komponenttia käytetään seuraavasti:
1029+
Komponentti saa siis propsina _onClick_ tapahtumankäsittelijän ja propsina _text_ merkkijonon, jonka se renderöi painikkeen tekstiksi. Komponenttia käytetään seuraavasti:
10321030

10331031
```js
10341032
const App = (props) => {
10351033
// ...
10361034
return (
10371035
<div>
10381036
{value}
1039-
<Button handleClick={() => setToValue(1000)} text="thousand" /> // highlight-line
1040-
<Button handleClick={() => setToValue(0)} text="reset" /> // highlight-line
1041-
<Button handleClick={() => setToValue(value + 1)} text="increment" /> // highlight-line
1037+
<Button onClick={() => setToValue(1000)} text="thousand" /> // highlight-line
1038+
<Button onClick={() => setToValue(0)} text="reset" /> // highlight-line
1039+
<Button onClick={() => setToValue(value + 1)} text="increment" /> // highlight-line
10421040
</div>
10431041
)
10441042
}
10451043
```
10461044

10471045
Komponentin <i>Button</i> käyttö on helppoa, mutta on toki pidettävä huolta siitä, että komponentille annettavat propsit on nimetty niin kuin komponentti olettaa:
10481046

1049-
![](../../images/1/12e.png)
1047+
![](../../images/1/12f.png)
10501048

10511049
### Älä määrittele komponenttia komponentin sisällä
10521050

@@ -1057,7 +1055,7 @@ Määritellään uusi komponentti <i>App</i>-komponentin sisällä:
10571055
```js
10581056
// tämä on oikea paikka määritellä komponentti!
10591057
const Button = (props) => (
1060-
<button onClick={props.handleClick}>
1058+
<button onClick={props.onClick}>
10611059
{props.text}
10621060
</button>
10631061
)
@@ -1076,9 +1074,9 @@ const App = (props) => {
10761074
return (
10771075
<div>
10781076
<Display value={value} /> // highlight-line
1079-
<Button handleClick={() => setToValue(1000)} text="thousand" />
1080-
<Button handleClick={() => setToValue(0)} text="reset" />
1081-
<Button handleClick={() => setToValue(value + 1)} text="increment" />
1077+
<Button onClick={() => setToValue(1000)} text="thousand" />
1078+
<Button onClick={() => setToValue(0)} text="reset" />
1079+
<Button onClick={() => setToValue(value + 1)} text="increment" />
10821080
</div>
10831081
)
10841082
}
@@ -1092,7 +1090,7 @@ Siirretäänkin komponentin <i>Display</i> määrittely oikeaan paikkaan eli kom
10921090
const Display = props => <div>{props.value}</div>
10931091

10941092
const Button = (props) => (
1095-
<button onClick={props.handleClick}>
1093+
<button onClick={props.onClick}>
10961094
{props.text}
10971095
</button>
10981096
)
@@ -1108,9 +1106,9 @@ const App = () => {
11081106
return (
11091107
<div>
11101108
<Display value={value} />
1111-
<Button handleClick={() => setToValue(1000)} text="thousand" />
1112-
<Button handleClick={() => setToValue(0)} text="reset" />
1113-
<Button handleClick={() => setToValue(value + 1)} text="increment" />
1109+
<Button onClick={() => setToValue(1000)} text="thousand" />
1110+
<Button onClick={() => setToValue(0)} text="reset" />
1111+
<Button onClick={() => setToValue(value + 1)} text="increment" />
11141112
</div>
11151113
)
11161114
}

src/content/images/1/12f.png

1.67 MB
Loading

0 commit comments

Comments
 (0)