|
1 |
| -ES6 class bodies are more terse than traditional object literals. Methods do not require a `function` keyword and no commas are needed to separate them. This refactoring looks as such: |
| 1 | +# Enforce ES5 or ES6 class for React Components (prefer-es6-class) |
2 | 2 |
|
3 |
| -Invalid: |
| 3 | +React offers you two way to create traditional components: using the ES5 `React.createClass` method or the new ES6 class system. This rule allow you to enforce one way or another. |
| 4 | + |
| 5 | +## Rule Options |
| 6 | + |
| 7 | +```js |
| 8 | +... |
| 9 | +"prefer-es6-class": [<enabled>, <mode>] |
| 10 | +... |
| 11 | +``` |
| 12 | + |
| 13 | +### `always` mode |
| 14 | + |
| 15 | +Will enforce ES6 classes for React Components. This is the default mode. |
| 16 | + |
| 17 | +The following patterns are considered warnings: |
4 | 18 |
|
5 | 19 | ```js
|
6 |
| -var ExampleComponent = React.createClass({ |
7 |
| - render: function() { |
8 |
| - return <div onClick={this._handleClick}>Hello, world.</div>; |
9 |
| - }, |
10 |
| - _handleClick: function() { |
11 |
| - console.log(this); |
12 |
| - } |
| 20 | +var Hello = React.createClass({ |
| 21 | + render: function() { |
| 22 | + return <div>Hello {this.props.name}</div>; |
| 23 | + } |
13 | 24 | });
|
14 | 25 | ```
|
15 | 26 |
|
16 |
| -Valid: |
| 27 | +The following patterns are not considered warnings: |
| 28 | + |
| 29 | +```js |
| 30 | +class Hello extends React.Component { |
| 31 | + render() { |
| 32 | + return <div>Hello {this.props.name}</div>; |
| 33 | + } |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +### `never` mode |
| 38 | + |
| 39 | +Will enforce ES5 classes for React Components |
| 40 | + |
| 41 | +The following patterns are considered warnings: |
17 | 42 |
|
18 | 43 | ```js
|
19 |
| -class ExampleComponent extends React.Component { |
20 |
| - render() { |
21 |
| - return <div onClick={this._handleClick}>Hello, world.</div>; |
22 |
| - } |
23 |
| - _handleClick() { |
24 |
| - console.log(this); |
25 |
| - } |
| 44 | +class Hello extends React.Component { |
| 45 | + render() { |
| 46 | + return <div>Hello {this.props.name}</div>; |
| 47 | + } |
26 | 48 | }
|
27 |
| -``` |
| 49 | +``` |
| 50 | + |
| 51 | +The following patterns are not considered warnings: |
| 52 | + |
| 53 | +```js |
| 54 | +var Hello = React.createClass({ |
| 55 | + render: function() { |
| 56 | + return <div>Hello {this.props.name}</div>; |
| 57 | + } |
| 58 | +}); |
| 59 | +``` |
0 commit comments