Skip to content

Commit 25cf8f3

Browse files
Merge pull request airbnb#4 from CondeNast/add-react-style
Add React style section to README
2 parents 83f9db2 + b5f1a98 commit 25cf8f3

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
1. [Constructors](#constructors)
2525
1. [Events](#events)
2626
1. [Modules](#modules)
27+
1. [React](#react)
2728
1. [jQuery](#jquery)
2829
1. [ECMAScript 5 Compatibility](#ecmascript-5-compatibility)
2930
1. [Testing](#testing)
@@ -1448,6 +1449,51 @@
14481449
**[⬆ back to top](#table-of-contents)**
14491450
14501451
1452+
## React
1453+
Our React style is mostly influenced by [David Chang's style guide](https://reactjsnews.com/react-style-guide-patterns-i-like/). Exceptions are enumerated below.
1454+
1455+
- Since `displayName` is automatically set by React after calling `React.createClass()` and transpiling from JSX to JavaScript, there's no need to explicitly include one:
1456+
1457+
```javascript
1458+
// Preferred
1459+
React.createClass({
1460+
propTypes: {},
1461+
mixins: [],
1462+
getInitialState: function () {},
1463+
componentWillMount: function () {},
1464+
componentWillUnmount: function () {},
1465+
render: function() {}
1466+
});
1467+
1468+
// Not preferred
1469+
React.createClass({
1470+
displayName: '',
1471+
propTypes: {},
1472+
mixins: [],
1473+
getInitialState: function () {},
1474+
componentWillMount: function () {},
1475+
componentWillUnmount: function () {},
1476+
render: function () {}
1477+
});
1478+
```
1479+
1480+
- When it comes to conditionals, we tend to prefer `&&` to ternaries and will often use `&&` rather than breaking logic out into separate methods:
1481+
1482+
```javascript
1483+
renderContent: function () {
1484+
var content =
1485+
<div>
1486+
{this.props.foo && <div className='foo' dangerouslySetInnerHTML={{__html: this.props.foo}} />}
1487+
{this.props.isTruthy && <MyComponent />}
1488+
{this.props.isTruthy && this.renderMyComponent()}
1489+
</div>;
1490+
1491+
return this.buildContent(content);
1492+
},
1493+
```
1494+
1495+
**[⬆ back to top](#table-of-contents)**
1496+
14511497
## jQuery
14521498
14531499
- Prefix jQuery object variables with a `$`.

0 commit comments

Comments
 (0)