From 4a4c61b6d2e35043623e99f24959f18ed7f8ab84 Mon Sep 17 00:00:00 2001 From: Vesa Jolkkonen Date: Sun, 12 Jan 2025 19:36:46 +0200 Subject: [PATCH] Clarify naming of the event handler props --- src/content/1/en/part1c.md | 27 ++------------------------- src/content/1/fi/osa1c.md | 22 +++++++++++----------- 2 files changed, 13 insertions(+), 36 deletions(-) diff --git a/src/content/1/en/part1c.md b/src/content/1/en/part1c.md index cd09f9d782..dc0fa5a226 100644 --- a/src/content/1/en/part1c.md +++ b/src/content/1/en/part1c.md @@ -628,10 +628,10 @@ const App = () => { Since we now have an easily reusable Button 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 Button 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 Button 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 @@ -729,11 +729,6 @@ const Button = (props) => { We can use destructuring to get only the required fields from props, 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 }) => ( -) -``` - -We can simplify the Button component once more by declaring the return statement in just one line: - -```js -const Button = ({ onSmash, text }) => -``` - -**NB**: However, be careful to not oversimplify your components, as this makes adding complexity a more tedious task down the road. - diff --git a/src/content/1/fi/osa1c.md b/src/content/1/fi/osa1c.md index fbbddd74ed..8d81dd5440 100644 --- a/src/content/1/fi/osa1c.md +++ b/src/content/1/fi/osa1c.md @@ -580,7 +580,7 @@ Tehdään seuraavaksi napeille tarkoitettu komponentti Button. Napille on ```js const Button = (props) => { return ( - ) @@ -602,15 +602,15 @@ const App = () => { // highlight-start ) @@ -715,8 +715,8 @@ const Button = (props) => { Destrukturoidaan props:ista tarpeelliset kentät ja käytetään nuolifunktioiden tiiviimpää muotoa: ```js -const Button = ({ handleClick, text }) => ( - )