|
13 | 13 | 1. [Props](#props) |
14 | 14 | 1. [Parentheses](#parentheses) |
15 | 15 | 1. [Tags](#tags) |
| 16 | + 1. [Methods](#methods) |
16 | 17 | 1. [Ordering](#ordering) |
17 | 18 |
|
18 | 19 | ## Basic Rules |
19 | 20 |
|
20 | 21 | - Only include one React component per file. |
21 | 22 | - Always use JSX syntax. |
22 | | - - Do not use `React.createElement` unless you're initializing the app from a `.js` file. |
| 23 | + - Do not use `React.createElement` unless you're initializing the app from a file that does not transform JSX. |
23 | 24 |
|
24 | 25 | ## Naming |
25 | 26 |
|
26 | | - - **Extensions**: Use `.jsx` extension for React components. |
27 | | - - **Filename**: Use PascalCase for filenames. E.g., `ReservationCard.jsx`. |
| 27 | + - **Extensions**: Use `.js` extension for React components. |
| 28 | + - **Filename**: Use PascalCase for filenames. E.g., `ReservationCard.js`. |
28 | 29 | - **Reference Naming**: Use PascalCase for React components and camelCase for their instances: |
29 | 30 | ```javascript |
30 | 31 | // bad |
|
40 | 41 | const reservationItem = <ReservationCard />; |
41 | 42 | ``` |
42 | 43 |
|
43 | | - **Component Naming**: Use the filename as the component name. So `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: |
| 44 | + **Component Naming**: Use the filename as the component name. So `ReservationCard.js` should have a reference name of ReservationCard. However, for root components of a directory, use index.js as the filename and use the directory name as the component name: |
44 | 45 | ```javascript |
45 | 46 | // bad |
46 | | - const Footer = require('./Footer/Footer.jsx') |
| 47 | + const Footer = require('./Footer/Footer.js') |
47 | 48 |
|
48 | 49 | // bad |
49 | | - const Footer = require('./Footer/index.jsx') |
| 50 | + const Footer = require('./Footer/index.js') |
50 | 51 |
|
51 | 52 | // good |
52 | 53 | const Footer = require('./Footer') |
|
71 | 72 | ``` |
72 | 73 |
|
73 | 74 | ## Alignment |
74 | | - - Follow these alignment styles for JSX syntax |
| 75 | + - Follow these alignment styles for js syntax |
75 | 76 |
|
76 | 77 | ```javascript |
77 | 78 | // bad |
78 | | - <Foo bar="bar" |
79 | | - baz="baz" /> |
| 79 | + <Foo superLongParam="bar" |
| 80 | + anotherSuperLongParam="baz" /> |
80 | 81 |
|
81 | 82 | // good |
82 | 83 | <Foo |
83 | | - bar="bar" |
84 | | - baz="baz" |
| 84 | + superLongParam="bar" |
| 85 | + anotherSuperLongParam="baz" |
85 | 86 | /> |
86 | 87 |
|
87 | 88 | // if props fit in one line then keep it on the same line |
88 | 89 | <Foo bar="bar" /> |
89 | 90 |
|
90 | 91 | // children get indented normally |
91 | 92 | <Foo |
92 | | - bar="bar" |
93 | | - baz="baz" |
| 93 | + superLongParam="bar" |
| 94 | + anotherSuperLongParam="baz" |
94 | 95 | > |
95 | 96 | <Spazz /> |
96 | 97 | </Foo> |
|
130 | 131 | ``` |
131 | 132 |
|
132 | 133 | ## Props |
133 | | - - Alphabetically order your props. |
| 134 | + - Always use camelCase for prop names. |
134 | 135 | ```javascript |
135 | 136 | // bad |
136 | 137 | <Foo |
137 | | - className="bar baz" |
138 | | - bar="bar" |
| 138 | + UserName="hello" |
| 139 | + phone_number={12345678} |
139 | 140 | /> |
140 | 141 |
|
141 | 142 | // good |
142 | 143 | <Foo |
143 | | - bar="bar" |
144 | | - className="bar baz" |
| 144 | + userName="hello" |
| 145 | + phoneNumber={12345678} |
145 | 146 | /> |
146 | 147 | ``` |
147 | 148 |
|
|
150 | 151 | ```javascript |
151 | 152 | /// bad |
152 | 153 | render() { |
153 | | - return <MyComponent className="long body" foo="bar" > |
| 154 | + return <MyComponent className="long body" foo="bar"> |
154 | 155 | <MyChild /> |
155 | 156 | </MyComponent>; |
156 | 157 | } |
|
195 | 196 | /> |
196 | 197 | ``` |
197 | 198 |
|
| 199 | +## Methods |
| 200 | + - Do not use underscore prefix for internal methods of a react component. |
| 201 | + ```javascript |
| 202 | + // bad |
| 203 | + React.createClass({ |
| 204 | + _onClickSubmit() { |
| 205 | + // do stuff |
| 206 | + } |
| 207 | +
|
| 208 | + // other stuff |
| 209 | + }); |
| 210 | +
|
| 211 | + // good |
| 212 | + React.createClass({ |
| 213 | + onClickSubmit() { |
| 214 | + // do stuff |
| 215 | + } |
| 216 | +
|
| 217 | + // other stuff |
| 218 | + }); |
| 219 | + ``` |
| 220 | + |
198 | 221 | ## Ordering |
199 | 222 | - Always follow the following order for methods in a react component: |
200 | 223 |
|
|
211 | 234 | 11. componentWillUpdate |
212 | 235 | 12. componentWillUnmount |
213 | 236 | 13. *clickHandlers or eventHandlers* like onClickSubmit() or onChangeDescription() |
214 | | - 14. *gettter methods for render* like getSelectReason() or getFooterContent() |
| 237 | + 14. *getter methods for render* like getSelectReason() or getFooterContent() |
215 | 238 | 15. *Optional render methods* like renderNavigation() or renderProfilePicture() |
216 | 239 | 16. render |
217 | 240 |
|
|
0 commit comments