Skip to content

Commit

Permalink
Clarify naming of the event handler props
Browse files Browse the repository at this point in the history
  • Loading branch information
vejol committed Jan 12, 2025
1 parent 94f6e0b commit 4a4c61b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 36 deletions.
27 changes: 2 additions & 25 deletions src/content/1/en/part1c.md
Original file line number Diff line number Diff line change
Expand Up @@ -628,10 +628,10 @@ const App = () => {
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.
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.
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.
React's own official [tutorial](https://react.dev/learn/tutorial-tic-tac-toe) suggests:
"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."
"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."
### Changes in state cause re-rendering
Expand Down Expand Up @@ -729,11 +729,6 @@ const Button = (props) => {
We can use destructuring to get only the required fields from <i>props</i>, and use the more compact form of arrow functions:
**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:
> By convention, event handler props should start with `on`, followed by a capital letter.
For example, the Button component’s `onClick` prop could have been called `onSmash`:
```js
const Button = ({ onClick, text }) => (
<button onClick={onClick}>
Expand All @@ -742,22 +737,4 @@ const Button = ({ onClick, text }) => (
)
```
could also be called as following:
```js
const Button = ({ onSmash, text }) => (
<button onClick={onSmash}>
{text}
</button>
)
```
We can simplify the Button component once more by declaring the return statement in just one line:
```js
const Button = ({ onSmash, text }) => <button onClick={onSmash}>{text}</button>
```
**NB**: However, be careful to not oversimplify your components, as this makes adding complexity a more tedious task down the road.
</div>
22 changes: 11 additions & 11 deletions src/content/1/fi/osa1c.md
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ Tehdään seuraavaksi napeille tarkoitettu komponentti <i>Button</i>. Napille on
```js
const Button = (props) => {
return (
<button onClick={props.handleClick}>
<button onClick={props.onClick}>
{props.text}
</button>
)
Expand All @@ -602,15 +602,15 @@ const App = () => {
<Display counter={counter}/>
// highlight-start
<Button
handleClick={increaseByOne}
onClick={increaseByOne}
text='plus'
/>
<Button
handleClick={setToZero}
onClick={setToZero}
text='zero'
/>
<Button
handleClick={decreaseByOne}
onClick={decreaseByOne}
text='minus'
/>
// highlight-end
Expand All @@ -621,7 +621,7 @@ const App = () => {
Koska meillä on nyt uudelleenkäytettävä komponentti <i>Button</i>, sovellukselle on lisätty uutena toiminnallisuutena nappi, jolla laskurin arvoa voi vähentää.
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.
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.
### Tilan muutos aiheuttaa uudelleenrenderöitymisen
Expand Down Expand Up @@ -658,9 +658,9 @@ const App = () => {
return (
<div>
<Display counter={counter} />
<Button handleClick={increaseByOne} text="plus" />
<Button handleClick={setToZero} text="zero" />
<Button handleClick={decreaseByOne} text="minus" />
<Button onClick={increaseByOne} text="plus" />
<Button onClick={setToZero} text="zero" />
<Button onClick={decreaseByOne} text="minus" />
</div>
)
}
Expand Down Expand Up @@ -705,7 +705,7 @@ Vastaava suoraviivaistus voidaan tehdä myös nappikomponentille:
```js
const Button = (props) => {
return (
<button onClick={props.handleClick}>
<button onClick={props.onClick}>
{props.text}
</button>
)
Expand All @@ -715,8 +715,8 @@ const Button = (props) => {
Destrukturoidaan <i>props</i>:ista tarpeelliset kentät ja käytetään nuolifunktioiden tiiviimpää muotoa:
```js
const Button = ({ handleClick, text }) => (
<button onClick={handleClick}>
const Button = ({ onClick, text }) => (
<button onClick={onClick}>
{text}
</button>
)
Expand Down

0 comments on commit 4a4c61b

Please sign in to comment.