Skip to content

Commit 2595cb6

Browse files
author
Alvin Sng
committed
removed alpha sort, changed jsx exten to js, added method prefix rule, added camelCase for props
1 parent 3764337 commit 2595cb6

1 file changed

Lines changed: 43 additions & 20 deletions

File tree

react/README.md

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,19 @@
1313
1. [Props](#props)
1414
1. [Parentheses](#parentheses)
1515
1. [Tags](#tags)
16+
1. [Methods](#methods)
1617
1. [Ordering](#ordering)
1718

1819
## Basic Rules
1920

2021
- Only include one React component per file.
2122
- 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.
2324

2425
## Naming
2526

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`.
2829
- **Reference Naming**: Use PascalCase for React components and camelCase for their instances:
2930
```javascript
3031
// bad
@@ -40,13 +41,13 @@
4041
const reservationItem = <ReservationCard />;
4142
```
4243

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:
4445
```javascript
4546
// bad
46-
const Footer = require('./Footer/Footer.jsx')
47+
const Footer = require('./Footer/Footer.js')
4748
4849
// bad
49-
const Footer = require('./Footer/index.jsx')
50+
const Footer = require('./Footer/index.js')
5051
5152
// good
5253
const Footer = require('./Footer')
@@ -71,26 +72,26 @@
7172
```
7273

7374
## Alignment
74-
- Follow these alignment styles for JSX syntax
75+
- Follow these alignment styles for js syntax
7576

7677
```javascript
7778
// bad
78-
<Foo bar="bar"
79-
baz="baz" />
79+
<Foo superLongParam="bar"
80+
anotherSuperLongParam="baz" />
8081
8182
// good
8283
<Foo
83-
bar="bar"
84-
baz="baz"
84+
superLongParam="bar"
85+
anotherSuperLongParam="baz"
8586
/>
8687
8788
// if props fit in one line then keep it on the same line
8889
<Foo bar="bar" />
8990
9091
// children get indented normally
9192
<Foo
92-
bar="bar"
93-
baz="baz"
93+
superLongParam="bar"
94+
anotherSuperLongParam="baz"
9495
>
9596
<Spazz />
9697
</Foo>
@@ -130,18 +131,18 @@
130131
```
131132

132133
## Props
133-
- Alphabetically order your props.
134+
- Always use camelCase for prop names.
134135
```javascript
135136
// bad
136137
<Foo
137-
className="bar baz"
138-
bar="bar"
138+
UserName="hello"
139+
phone_number={12345678}
139140
/>
140141
141142
// good
142143
<Foo
143-
bar="bar"
144-
className="bar baz"
144+
userName="hello"
145+
phoneNumber={12345678}
145146
/>
146147
```
147148

@@ -150,7 +151,7 @@
150151
```javascript
151152
/// bad
152153
render() {
153-
return <MyComponent className="long body" foo="bar" >
154+
return <MyComponent className="long body" foo="bar">
154155
<MyChild />
155156
</MyComponent>;
156157
}
@@ -195,6 +196,28 @@
195196
/>
196197
```
197198

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+
198221
## Ordering
199222
- Always follow the following order for methods in a react component:
200223

@@ -211,7 +234,7 @@
211234
11. componentWillUpdate
212235
12. componentWillUnmount
213236
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()
215238
15. *Optional render methods* like renderNavigation() or renderProfilePicture()
216239
16. render
217240

0 commit comments

Comments
 (0)