|
56 | 56 |
|
57 | 57 | ```javascript |
58 | 58 | // bad |
59 | | - const reservationCard = require('./ReservationCard'); |
| 59 | + import reservationCard from './ReservationCard'; |
60 | 60 |
|
61 | 61 | // good |
62 | | - const ReservationCard = require('./ReservationCard'); |
| 62 | + import ReservationCard from './ReservationCard'; |
63 | 63 |
|
64 | 64 | // bad |
65 | 65 | const ReservationItem = <ReservationCard />; |
|
68 | 68 | const reservationItem = <ReservationCard />; |
69 | 69 | ``` |
70 | 70 |
|
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: |
72 | 72 |
|
73 | 73 | ```javascript |
74 | 74 | // bad |
75 | | - const Footer = require('./Footer/Footer.jsx') |
| 75 | + import Footer from './Footer/Footer'; |
76 | 76 |
|
77 | 77 | // bad |
78 | | - const Footer = require('./Footer/index.jsx') |
| 78 | + import Footer from './Footer/index'; |
79 | 79 |
|
80 | 80 | // good |
81 | | - const Footer = require('./Footer') |
| 81 | + import Footer from './Footer'; |
82 | 82 | ``` |
83 | 83 |
|
84 | 84 | ## Declaration |
|
186 | 186 | /> |
187 | 187 | ``` |
188 | 188 |
|
| 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 | +
|
189 | 205 | ## Parentheses |
190 | 206 |
|
191 | 207 | - Wrap JSX tags in parentheses when they span more than one line. |
192 | 208 |
|
193 | 209 | eslint rules: [`react/wrap-multilines`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/wrap-multilines.md). |
194 | 210 |
|
195 | 211 | ```javascript |
196 | | - /// bad |
| 212 | + // bad |
197 | 213 | render() { |
198 | 214 | return <MyComponent className="long body" foo="bar"> |
199 | 215 | <MyChild /> |
|
249 | 265 |
|
250 | 266 | ## Methods |
251 | 267 |
|
| 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 | +
|
252 | 304 | - Do not use underscore prefix for internal methods of a React component. |
253 | 305 |
|
254 | 306 | ```javascript |
255 | 307 | // bad |
256 | 308 | React.createClass({ |
257 | 309 | _onClickSubmit() { |
258 | 310 | // do stuff |
259 | | - } |
| 311 | + }, |
260 | 312 |
|
261 | 313 | // other stuff |
262 | 314 | }); |
|
268 | 320 | } |
269 | 321 |
|
270 | 322 | // other stuff |
271 | | - }); |
| 323 | + } |
272 | 324 | ``` |
273 | 325 |
|
274 | 326 | ## Ordering |
|
0 commit comments