## React code specific rules### Enforce consistent naming for the events of React componentsWhat: Check that every property of a React component which is a callback function is named according to the event which triggers the function. In addition the name of these properties should always start with the word "on".Why: This rule makes sure that all properties on our React components that are event callbacks follow the standard HTML naming convention.Example 1 - Correct code - The onOkClick property is named according to the click event of the OK button:```tsx/** Type for the properties of the MessageBox React component. */type MessageBoxProps = { /** The text displayed in the message box. */ text: string; /** The event executed when the user clicks the OK button in the message box. */ onOkClick: () => void;};/** The React component for the message box. */class MessageBox extends React.Component<MessageBoxProps> { public override render(): React.ReactNode { return ( <div> <span>{this.props.text}</span> <button type="button" onClick={this.props.onOkClick}> OK </button> </div> ); }}```Example 2 - Wrong code - The performSomeTask property is not named according to the event it is triggered on:```tsx/** Type for the properties of the MessageBox React component. */type MessageBoxProps = { /** The text displayed in the message box. */ text: string; /** The event executed when the user clicks the OK button in the message box. */ performSomeTask: () => void;};/** The React component for the message box. */class MessageBox extends React.Component<MessageBoxProps> { public override render(): React.ReactNode { return ( <div> <span>{this.props.text}</span> <button type="button" onClick={this.props.performSomeTask}> OK </button> </div> ); }}```
Hey @Krisztián Balla
Thank you for sharing this insight and your configuration! You’ve highlighted a best practice that’s often overlooked, especially by developers new to React or component-based UI design.
Your configuration and examples are spot-on.
on
If you want to automate this rule in your codebase, consider using <a href="https://eslint.org/" target="_blank" rel="nofollow noopener noreferrer">https://eslint.org/</a> with plugins like GitHub - jsx-eslint/eslint-plugin-react: React-specific linting rules for ESLint or <a href="https://www.npmjs.com/package/eslint-plugin-react-hooks" target="_blank" rel="nofollow noopener noreferrer">https://www.npmjs.com/package/eslint-plugin-react-hooks</a>. While there isn’t a built-in rule for this exact pattern, you can write a <a href="https://eslint.org/docs/latest/developer-guide/working-with-rules" target="_blank" rel="nofollow noopener noreferrer">https://eslint.org/docs/latest/developer-guide/working-with-rules</a> or use code reviews to enforce it.
<a href="https://eslint.org/" target="_blank" rel="nofollow noopener noreferrer">https://eslint.org/</a>
<a href="https://www.npmjs.com/package/eslint-plugin-react-hooks" target="_blank" rel="nofollow noopener noreferrer">https://www.npmjs.com/package/eslint-plugin-react-hooks</a>
<a href="https://eslint.org/docs/latest/developer-guide/working-with-rules" target="_blank" rel="nofollow noopener noreferrer">https://eslint.org/docs/latest/developer-guide/working-with-rules</a>
Keep sharing tips if you have more! Thank you for participating in the community!
Kindest regards,
Jov
It looks like you're new here. Sign in or register to get started.