.eslintrc.json
{
"extends": "@mgtitimoli/eslint-config-react/general"
}
Disallows the declaration of more than one custom component in the same file
{
"react/no-multi-comp": "error"
}
Disallows the use of strings in refs (this has been deprecated), enforcing the use of functions (to store the instance) in their place
{
"react/no-string-refs": "error"
}
// BAD
import React, { Component } from "react";
class CustomInput extends Component {
// ...
render() {
return (
<input ref="input"/>
);
}
}
// GOOD
import React, { Component } from "react";
class CustomInput extends Component {
// ...
_input = null;
_setInput(input) {
this._input = input;
}
render() {
return (
<input ref={ this._setInput.bind(this) }/>
);
}
}
Disallows the use of React.createClass, encouraging the declaration of components using the ES2015 alternative.
{
"react/prefer-es6-class": "error"
}
// BAD
import React from "react";
const MyComponent = React.createClass({
// ...
});
// GOOD
import React, { Component } from "react";
class MyComponent extends Component {
// ...
}
Warns when using props without having declared their types in propTypes
{
"react/prop-types": "warn"
}
// BAD
import React, { Component } from "react";
class MyComponent extends Component {
render() {
return (
<div className={ this.props.className }/>
);
}
}
// GOOD
import React, {
Component,
PropTypes
} from "react";
class MyComponent extends Component{
static propTypes = {
className: PropTypes.string
};
render() {
return (
<div className={ this.props.className }/>
);
}
}
Ensures the presence of a return statement in render methods
{
"react/require-render-return": "error"
}
// BAD
class MyComponent extends Component {
render() {
<div/>;
}
}
// GOOD
class MyComponent extends Component {
render() {
return (
<div/>
);
}
}
Prevents the use of unnecessary closing tags for components without children
{
"react/self-closing-comp": "error"
}
// BAD
class MyComponent extends Component {
render() {
return (
<div></div>
);
}
}
// GOOD
class MyComponent extends Component {
render() {
return (
<div/>
);
}
}
Enforces the following component members ordering:
- Static
- Lifecycle
- displayName
- propTypes
- contextTypes
- childContextTypes
- defaultProps
- constructor
- state
- getChildContext
- componentWillMount
- componentDidMount
- componentWillReceiveProps
- shouldComponentUpdate
- componentWillUpdate
- componentDidUpdate
- componentWillUnmount
- General purpose
- Handlers (start with _handler)
- Rendering (start with _render)
{
"react/sort-comp": [ "error", {
"order": [
"static-methods",
"lifecycle",
"everything-else",
"handlers",
"rendering"
],
"groups": {
"handlers": [
"/^_handle.+$/"
],
"rendering": [
"/^_render.+$/",
"render"
]
}
} ]
}
Enforces enclosing multiline components with parentheses
{
"react/wrap-multilines": "error"
}
// BAD
const content = <div>
<p>Some text</p>
</div>;
// GOOD
const content = (
<div>
<p>Some text</p>
</div>
);
Enforces prop types to be alphabetically sorted
{
"react/sort-prop-types": "error"
}
// BAD
class MyComponent extends Component {
static propTypes = {
c: PropTypes.string,
a: PropTypes.string,
b: PropTypes.string
}
// ...
}
// GOOD
class MyComponent extends Component {
static propTypes = {
a: PropTypes.string,
b: PropTypes.string,
c: PropTypes.string
}
// ...
}