Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(style): add section for naming event handlers #4880

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions docs/style.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,65 @@ MyComponent.propTypes = {
_Note: not every component will mirror the structure above. Some will need to
incorporate `useEffect`, some will not. You can think of the outline above as
slots that you can fill if you need this functionality in a component._

### Style

#### Naming event handlers

<table>
<thead><tr><th>Unpreferred</th><th>Preferred</th></tr></thead>
<tbody>
<tr><td>

```jsx
function MyComponent() {
function click() {
// ...
}
return <button onClick={click} />;
}
```

</td><td>

```jsx
function MyComponent() {
function onClick() {
// ...
}
return <button onClick={onClick} />;
}
```

</td></tr>
<tr><td>

```jsx
function MyComponent({ onClick }) {
function handleClick(event) {
// ...
onClick(event);
}
return <button onClick={handleClick} />;
}
```

</td><td>

```jsx
function MyComponent({ onClick }) {
function handleOnClick(event) {
// ...
onClick(event);
}
return <button onClick={handleOnClick} />;
}
```

</td></tr>
</tbody></table>

When writing event handlers, we prefer using the exact name, `onClick` to a
shorthand. If that name is already being used in a given scope, which often
happens if the component supports a prop `onClick`, then we prefer to specify
the function as `handleOnClick`.