Skip to content

Commit 4a4c61b

Browse files
committed
Clarify naming of the event handler props
1 parent 94f6e0b commit 4a4c61b

File tree

2 files changed

+13
-36
lines changed

2 files changed

+13
-36
lines changed

src/content/1/en/part1c.md

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -628,10 +628,10 @@ const App = () => {
628628
629629
Since we now have an easily reusable <i>Button</i> component, we've also implemented new functionality into our application by adding a button that can be used to decrement the counter.
630630
631-
The event handler is passed to the <i>Button</i> component through the _onClick_ prop. The name of the prop itself is not that significant, but our naming choice wasn't completely random.
631+
The event handler is passed to the <i>Button</i> component through the _onClick_ prop. When creating your own components, you can theoretically choose the prop name freely. However, our naming choice for the event handler was not entirely arbitrary.
632632
633633
React's own official [tutorial](https://react.dev/learn/tutorial-tic-tac-toe) suggests:
634-
"In React, it’s conventional to use onSomething names for props which take functions which handle events and handleSomething for the actual function definitions which handle those events."
634+
"In React, it’s conventional to use _onSomething_ names for props which take functions which handle events and handleSomething for the actual function definitions which handle those events."
635635
636636
### Changes in state cause re-rendering
637637
@@ -729,11 +729,6 @@ const Button = (props) => {
729729
730730
We can use destructuring to get only the required fields from <i>props</i>, and use the more compact form of arrow functions:
731731
732-
**NB**: While building your own components, you can name their event handler props anyway you like, for this you can refer to the react's documentation on [Naming event handler props](https://react.dev/learn/responding-to-events#naming-event-handler-props). It goes as following:
733-
734-
> By convention, event handler props should start with `on`, followed by a capital letter.
735-
For example, the Button component’s `onClick` prop could have been called `onSmash`:
736-
737732
```js
738733
const Button = ({ onClick, text }) => (
739734
<button onClick={onClick}>
@@ -742,22 +737,4 @@ const Button = ({ onClick, text }) => (
742737
)
743738
```
744739
745-
could also be called as following:
746-
747-
```js
748-
const Button = ({ onSmash, text }) => (
749-
<button onClick={onSmash}>
750-
{text}
751-
</button>
752-
)
753-
```
754-
755-
We can simplify the Button component once more by declaring the return statement in just one line:
756-
757-
```js
758-
const Button = ({ onSmash, text }) => <button onClick={onSmash}>{text}</button>
759-
```
760-
761-
**NB**: However, be careful to not oversimplify your components, as this makes adding complexity a more tedious task down the road.
762-
763740
</div>

src/content/1/fi/osa1c.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ Tehdään seuraavaksi napeille tarkoitettu komponentti <i>Button</i>. Napille on
580580
```js
581581
const Button = (props) => {
582582
return (
583-
<button onClick={props.handleClick}>
583+
<button onClick={props.onClick}>
584584
{props.text}
585585
</button>
586586
)
@@ -602,15 +602,15 @@ const App = () => {
602602
<Display counter={counter}/>
603603
// highlight-start
604604
<Button
605-
handleClick={increaseByOne}
605+
onClick={increaseByOne}
606606
text='plus'
607607
/>
608608
<Button
609-
handleClick={setToZero}
609+
onClick={setToZero}
610610
text='zero'
611611
/>
612612
<Button
613-
handleClick={decreaseByOne}
613+
onClick={decreaseByOne}
614614
text='minus'
615615
/>
616616
// highlight-end
@@ -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 _handleClick_ välityksellä. Propsin nimellä ei ole sinänsä merkitystä, mutta valinta ei ollut sattumanvarainen, sillä esim. Reactin [tutoriaali](https://react.dev/learn/tutorial-tic-tac-toe) suosittelee tätä konventiota.
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.
625625
626626
### Tilan muutos aiheuttaa uudelleenrenderöitymisen
627627
@@ -658,9 +658,9 @@ const App = () => {
658658
return (
659659
<div>
660660
<Display counter={counter} />
661-
<Button handleClick={increaseByOne} text="plus" />
662-
<Button handleClick={setToZero} text="zero" />
663-
<Button handleClick={decreaseByOne} text="minus" />
661+
<Button onClick={increaseByOne} text="plus" />
662+
<Button onClick={setToZero} text="zero" />
663+
<Button onClick={decreaseByOne} text="minus" />
664664
</div>
665665
)
666666
}
@@ -705,7 +705,7 @@ Vastaava suoraviivaistus voidaan tehdä myös nappikomponentille:
705705
```js
706706
const Button = (props) => {
707707
return (
708-
<button onClick={props.handleClick}>
708+
<button onClick={props.onClick}>
709709
{props.text}
710710
</button>
711711
)
@@ -715,8 +715,8 @@ const Button = (props) => {
715715
Destrukturoidaan <i>props</i>:ista tarpeelliset kentät ja käytetään nuolifunktioiden tiiviimpää muotoa:
716716
717717
```js
718-
const Button = ({ handleClick, text }) => (
719-
<button onClick={handleClick}>
718+
const Button = ({ onClick, text }) => (
719+
<button onClick={onClick}>
720720
{text}
721721
</button>
722722
)

0 commit comments

Comments
 (0)