You can write a class component with class or with createReactClass(). The two differ in how methods are bound and how the initial state and the default props are set, so a codebase that mixes them is harder to read. By default, the rule requires class.
Configure
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"react/prefer-es6-class" = "error" # or "warn"
Every option, set to its default:
bunfig.toml
[lint.rules]
"react/prefer-es6-class" = ["error", "always"] # "always" or "never"
Examples
Reported:
const Greeting = createReactClass({
render() {
return <h1>Hello {this.props.name}</h1>;
},
});
Not reported:
class Greeting extends React.Component {
render() {
return <h1>Hello {this.props.name}</h1>;
}
}
Options
| Option | What it does |
|---|---|
"always" (default) | Report components made with createReactClass(). |
"never" | Report components written as a class declaration. |
Reported with "never":
class Greeting extends React.Component {
render() {
return <h1>Hello {this.props.name}</h1>;
}
}