react/prefer-es6-class
Enforce one way to write class components: class or createReactClass
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#
example.jsx1 problem
const Greeting = createReactClass({Component should use es6 class instead of createClass
render() {
return <h1>Hello {this.props.name}</h1>;
},
});example.jsxNo problems
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":
example.jsxwith
"never"1 problemclass Greeting extends React.Component {Component should use createClass instead of es6 class
render() {
return <h1>Hello {this.props.name}</h1>;
}
}