diff --git a/docs/style.md b/docs/style.md index e62f0bcdc396..5e0491648cd7 100644 --- a/docs/style.md +++ b/docs/style.md @@ -207,3 +207,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 + + + + + + +
UnpreferredPreferred
+ +```jsx +function MyComponent() { + function click() { + // ... + } + return + +```jsx +function MyComponent() { + function onClick() { + // ... + } + return
+ +```jsx +function MyComponent({ onClick }) { + function handleClick(event) { + // ... + onClick(event); + } + return + +```jsx +function MyComponent({ onClick }) { + function handleOnClick(event) { + // ... + onClick(event); + } + return
+ +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`.