react/jsx-handler-names
Enforce naming conventions for event handlers and the props they are passed to
In a common React convention, a prop that takes an event handler starts with on, and the function that handles the event starts with handle. The rule reports an on prop that gets a function with another name, and a handle function that's passed to a prop with another name. Passing a prop through, as in onBlur={props.onBlur}, is fine.
By default the rule only checks members such as this.handleChange. It skips local variables and inline functions, and it never checks ref.
Configure#
Off by default. To turn it on:
[lint.rules]
"react/jsx-handler-names" = "error" # or "warn"Every option, set to its default:
[lint.rules]
"react/jsx-handler-names" = ["error", {
eventHandlerPrefix = "handle",
eventHandlerPropPrefix = "on",
checkLocalVariables = false,
checkInlineFunction = false,
ignoreComponentNames = [], # list of strings
}]Examples#
Reported:
class Form extends React.Component {
render() {
return <Input onChange={this.updateName} blur={this.handleBlur} />;
}
}Not reported:
class Form extends React.Component {
render() {
return (
<Input
onChange={this.handleChange}
onBlur={this.props.onBlur}
onFocus={() => this.track("focus")}
ref={this.handleRef}
/>
);
}
}Options#
| Option | Default | What it does |
|---|---|---|
eventHandlerPrefix | "handle" | The prefix for a handler function. |
eventHandlerPropPrefix | "on" | The prefix for a prop that takes a handler. |
checkLocalVariables | false | Also check handlers that are plain variables, such as onChange={updateName}. |
checkInlineFunction | false | Also check the function that an arrow function calls, such as onChange={() => this.updateName()}. |
ignoreComponentNames | [] | Components to skip. A name can be a glob pattern. |
Function components keep their handlers in local variables, so they need checkLocalVariables:
function Form() {
const updateName = event => setName(event.target.value);
return <Input onChange={updateName} />;
}function Form() {
const handleChange = event => setName(event.target.value);
return <Input onChange={handleChange} />;
}With checkInlineFunction:
const field = <Input onChange={() => this.updateName()} />;With other prefixes, here "do" and "when":
const field = <Input whenChange={this.handleChange} />;With ignoreComponentNames set to ["Legacy*"]:
const field = <LegacyInput onChange={this.updateName} />;Either prefix can also be false. Both checks need both prefixes, so that turns the whole rule off:
const field = <Input onChange={this.updateName} blur={this.handleBlur} />;const field = <Input onChange={this.updateName} blur={this.handleBlur} />;