react/no-unused-class-component-methods
Disallow methods and properties of a class component that the class never uses
A handler that's no longer wired to any element stays in the class and looks like it still matters. This rule reports a method or a property of a class component, or of a createReactClass component, that's never read through this in the same component.
Lifecycle methods such as render and componentDidMount, state, and static members are never reported.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"react/no-unused-class-component-methods" = "error" # or "warn"Examples#
example.jsx2 problems
class SearchBox extends React.Component {
timeoutUnused method or property timeout of class SearchBox = 300;
handleChangeUnused method or property handleChange of class SearchBox(event) {
this.props.onSearch(event.target.value);
}
render() {
return <input />;
}
}example.jsxNo problems
class SearchBox extends React.Component {
timeout = 300;
handleChange = event => {
setTimeout(() => this.props.onSearch(event.target.value), this.timeout);
};
render() {
return <input onChange={this.handleChange} />;
}
}When not to use it#
The rule only looks inside the component. If a parent calls a method through a ref, such as searchBox.current.focus(), the method is reported even though it's used.