Skip to content

Commit

Permalink
Cleaning up the react styleguide. Adding additional info in props.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan Gensler committed Dec 14, 2015
1 parent 5989125 commit 5a2d659
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 10 deletions.
3 changes: 3 additions & 0 deletions packages/eslint-config-airbnb/rules/react.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ module.exports = {
// Validate props indentation in JSX
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-indent-props.md
'react/jsx-indent-props': [2, 2],
// Prevent usage of .bind() and arrow functions in JSX props
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-no-bind.md
'react/jsx-no-bind': 2,
// Prevent duplicate props in JSX
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-no-duplicate-props.md
'react/jsx-no-duplicate-props': 0,
Expand Down
117 changes: 107 additions & 10 deletions react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

## Class vs React.createClass

- Use class extends React.Component unless you have a very good reason to use mixins.
- Use `class extends React.Component` unless you have a very good reason to use mixins.

eslint rules: [`react/prefer-es6-class`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/prefer-es6-class.md).

Expand Down Expand Up @@ -54,10 +54,10 @@

```javascript
// bad
const reservationCard = require('./ReservationCard');
import reservationCard from './ReservationCard';

// good
const ReservationCard = require('./ReservationCard');
import ReservationCard from './ReservationCard';

// bad
const ReservationItem = <ReservationCard />;
Expand All @@ -69,13 +69,13 @@
**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:
```javascript
// bad
const Footer = require('./Footer/Footer.jsx')
import Footer from './Footer/Footer.jsx';
// bad
const Footer = require('./Footer/index.jsx')
import Footer from './Footer/index.jsx';
// good
const Footer = require('./Footer')
import Footer from './Footer';
```


Expand Down Expand Up @@ -177,13 +177,29 @@
/>
```
- Omit the value of the prop when it is explicitly `true`.
eslint rules: [`react/jsx-boolean-value`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-boolean-value.md).
```javascript
// bad
<Foo
hidden={true}
/>
// good
<Foo
hidden
/>
```
## Parentheses
- Wrap JSX tags in parentheses when they span more than one line.
eslint rules: [`react/wrap-multilines`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/wrap-multilines.md).
```javascript
/// bad
// bad
render() {
return <MyComponent className="long body" foo="bar">
<MyChild />
Expand Down Expand Up @@ -237,16 +253,52 @@
```
## Methods
- Bind event handlers for the render method in the constructor.
> Why? A bind call in a prop will create a brand new function on every single render.
eslint rules: [`react/jsx-no-bind`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-no-bind.md).
```javascript
// bad
class extends React.Component {
onClickDiv() {
// do stuff
}
render() {
return <div onClick={this.onClickDiv.bind(this)} />
}
}
// good
class extends React.Component {
constructor(props) {
super(props);
this.onClickDiv = this.onClickDiv.bind(this);
}
onClickDiv() {
// do stuff
}
render() {
return <div onClick={this.onClickDiv} />
}
}
```
- Do not use underscore prefix for internal methods of a React component.
```javascript
// bad
React.createClass({
class extends React.Component {
_onClickSubmit() {
// do stuff
}
// other stuff
});
}
// good
class extends React.Component {
Expand All @@ -255,7 +307,52 @@
}
// other stuff
});
}
```
- Prefix the name of event handlers with `on`.
```javascript
// bad
class extends React.Component {
handleClickDiv() {
// do stuff
}
// other stuff
}
// good
class extends React.Component {
onClickDiv() {
// do stuff
}
// other stuff
}
```
- Prefix the name of additional render methods with `render`.
```javascript
// bad
class extends React.Component {
createNavigation(){
// render stuff
}
render() {
{this.createNavigation()}
}
}
// good
class extends React.Component {
renderNavigation(){
// render stuff
}
render() {
{this.renderNavigation()}
}
}
```
## Ordering
Expand Down

0 comments on commit 5a2d659

Please sign in to comment.