Skip to content

Commit fcd1c1d

Browse files
committed
Merge pull request airbnb#619 from kesne/jgens/react-cleanup
[eslint config] [breaking] [react] Minor cleanup for the React styleguide
2 parents 8815205 + e12e5f0 commit fcd1c1d

File tree

2 files changed

+64
-9
lines changed

2 files changed

+64
-9
lines changed

packages/eslint-config-airbnb/rules/react.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ module.exports = {
2121
// Validate props indentation in JSX
2222
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-indent-props.md
2323
'react/jsx-indent-props': [2, 2],
24+
// Prevent usage of .bind() and arrow functions in JSX props
25+
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-no-bind.md
26+
'react/jsx-no-bind': 2,
2427
// Prevent duplicate props in JSX
2528
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-no-duplicate-props.md
2629
'react/jsx-no-duplicate-props': 0,

react/README.md

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@
5656

5757
```javascript
5858
// bad
59-
const reservationCard = require('./ReservationCard');
59+
import reservationCard from './ReservationCard';
6060
6161
// good
62-
const ReservationCard = require('./ReservationCard');
62+
import ReservationCard from './ReservationCard';
6363
6464
// bad
6565
const ReservationItem = <ReservationCard />;
@@ -68,17 +68,17 @@
6868
const reservationItem = <ReservationCard />;
6969
```
7070

71-
**Component Naming**: Use the filename as the component name. For example, `ReservationCard.jsx` should have a reference name of `ReservationCard`. However, for root components of a directory, use `index.jsx` as the filename and use the directory name as the component name:
71+
- **Component Naming**: Use the filename as the component name. For example, `ReservationCard.jsx` should have a reference name of `ReservationCard`. However, for root components of a directory, use `index.jsx` as the filename and use the directory name as the component name:
7272

7373
```javascript
7474
// bad
75-
const Footer = require('./Footer/Footer.jsx')
75+
import Footer from './Footer/Footer';
7676
7777
// bad
78-
const Footer = require('./Footer/index.jsx')
78+
import Footer from './Footer/index';
7979
8080
// good
81-
const Footer = require('./Footer')
81+
import Footer from './Footer';
8282
```
8383

8484
## Declaration
@@ -186,14 +186,30 @@
186186
/>
187187
```
188188
189+
- Omit the value of the prop when it is explicitly `true`.
190+
191+
eslint rules: [`react/jsx-boolean-value`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-boolean-value.md).
192+
193+
```javascript
194+
// bad
195+
<Foo
196+
hidden={true}
197+
/>
198+
199+
// good
200+
<Foo
201+
hidden
202+
/>
203+
```
204+
189205
## Parentheses
190206
191207
- Wrap JSX tags in parentheses when they span more than one line.
192208
193209
eslint rules: [`react/wrap-multilines`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/wrap-multilines.md).
194210
195211
```javascript
196-
/// bad
212+
// bad
197213
render() {
198214
return <MyComponent className="long body" foo="bar">
199215
<MyChild />
@@ -249,14 +265,50 @@
249265
250266
## Methods
251267
268+
- Bind event handlers for the render method in the constructor.
269+
270+
> Why? A bind call in a the render path create a brand new function on every single render.
271+
272+
eslint rules: [`react/jsx-no-bind`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-no-bind.md).
273+
274+
```javascript
275+
// bad
276+
class extends React.Component {
277+
onClickDiv() {
278+
// do stuff
279+
}
280+
281+
render() {
282+
return <div onClick={this.onClickDiv.bind(this)} />
283+
}
284+
}
285+
286+
// good
287+
class extends React.Component {
288+
constructor(props) {
289+
super(props);
290+
291+
this.onClickDiv = this.onClickDiv.bind(this);
292+
}
293+
294+
onClickDiv() {
295+
// do stuff
296+
}
297+
298+
render() {
299+
return <div onClick={this.onClickDiv} />
300+
}
301+
}
302+
```
303+
252304
- Do not use underscore prefix for internal methods of a React component.
253305
254306
```javascript
255307
// bad
256308
React.createClass({
257309
_onClickSubmit() {
258310
// do stuff
259-
}
311+
},
260312
261313
// other stuff
262314
});
@@ -268,7 +320,7 @@
268320
}
269321
270322
// other stuff
271-
});
323+
}
272324
```
273325
274326
## Ordering

0 commit comments

Comments
 (0)