Skip to content

Commit 2f37fe6

Browse files
committed
changed js for ts, removed several class examples and changed folder structure
1 parent 60964a5 commit 2f37fe6

File tree

1 file changed

+76
-99
lines changed

1 file changed

+76
-99
lines changed

frontend/react/style-guide.md

+76-99
Original file line numberDiff line numberDiff line change
@@ -37,62 +37,46 @@ src
3737
└───app
3838
│ │
3939
│ └───components
40-
│ │ └───atoms # Can't be broken down into smaller things
41-
│ │ | └───Input
42-
│ │ | └───Text
43-
│ │ | └───Button
44-
│ │ | └───etc
45-
│ │ └───molecules # Two or more atoms
46-
│ │ | └───FormInput
47-
│ │ | └───SearchBar
48-
│ │ | └───Table
49-
│ │ | └───etc
50-
│ │ └───organisms # Two or more molecules
51-
│ │ └───LoginForm
52-
│ │ └───UserTable
53-
│ │ └───etc
40+
│ | └───FormInput
41+
│ | └───SearchBar
42+
│ | └───Table
43+
│ | └───etc
44+
| |
5445
│ └───screens
5546
│ └───MyScreenComponent
5647
│ └───assets # Screen specific app assets
57-
│ └───reducer
58-
│ | actions.js
59-
│ | reducer.js
60-
│ | selectors.js
61-
│ | effects.js
48+
│ └───context
49+
| | index.ts
50+
│ | actions.ts
51+
│ | reducer.ts
6252
│ └───components # Screen specific components
63-
│ | constants.js
64-
│ | i18n.js
65-
│ | index.js
66-
│ | layout.js
53+
│ | constants.ts
54+
│ | i18n.ts
55+
│ | index.tsx
6756
│ | styles.scss
68-
│ | utils.js
57+
│ | utils.ts
6958
7059
└───assets # General app assets
7160
└───config
72-
| api.js
73-
| i18n.js
61+
| api.ts
62+
| i18n.ts
7463
└───constants
75-
└───redux
76-
│ | store.js
77-
│ └───myReducer
78-
│ | actions.js
79-
│ | reducer.js
80-
│ | selectors.js
81-
│ | effects.js
82-
83-
└───propTypes
84-
│ | Model1.js
85-
│ │ Model2.js
64+
└───types # Custom global TypeScript classes
65+
└───contexts # Global contexts
66+
│ └───Auth
67+
│ | index.ts
68+
| | reducer.ts
69+
| | actions.ts
8670
8771
└───scss
8872
└───services
89-
│ │ serializers.js
73+
│ │ serializers.ts
9074
│ └───Model
91-
│ | ModelService.js
92-
│ | serializers.js
75+
│ | ModelService.ts
76+
│ | serializers.ts
9377
9478
└───utils
95-
│ index.js
79+
│ index.tsx
9680
```
9781

9882
## Class vs `React.createClass` vs stateless
@@ -175,18 +159,18 @@ src
175159

176160
## Naming
177161

178-
- **Extensions**: Use `.js` extension for React components.
179-
- **Filename**: For services use PascalCase. E.g., `ReservationCard.js` or a folder with the service name and `index.js` as filename. For React components, there must be a folder in PascalCase with its name and the component file should be `index.js` (and optionally `layout.js` if you're using [Smart/Dumb components](https://medium.com/@thejasonfile/dumb-components-and-smart-components-e7b33a698d43)
162+
- **Extensions**: Use `.ts` extension for React components.
163+
- **Filename**: For services use PascalCase. E.g., `ReservationCard.ts` or a folder with the service name and `index.tsx` as filename. For React components, there must be a folder in PascalCase with its name and the component file should be `index.tsx`
180164
- **Reference Naming**: Use PascalCase for React components and camelCase for their associated elements. eslint: [`react/jsx-pascal-case`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-pascal-case.md)
181165

182166
```jsx
183167
// bad
184168
import myService from './MyService';
185-
import myComponent from './MyComponent';
169+
import myComponent from './MyComponent/index.tsx';
186170
187171
// good
188-
import MyService from './MyService'; # webpack infers index.js
189-
import MyComponent from './MyComponent'; # webpack infers index.js
172+
import MyService from './MyService'; # webpack infers index.tsx
173+
import MyComponent from './MyComponent'; # webpack infers index.tsx
190174
191175
// bad
192176
const MyComponent = <MyComponent />;
@@ -196,26 +180,17 @@ src
196180
```
197181
- **Component Hierarchy**:
198182
- Component files should be inside folders that match the component's name.
199-
- Use index.js as the filename of a container component. Use `Container` as the suffix of the component's name.
200-
- Use layout.js as the filename of a layout component. Only do this if your component is becoming too big, it should be an exception, not a rule.
183+
- Use index.tsx as the component filename.
201184
202185
203186
```jsx
204-
// MyComponent/index.js
205-
import MyComponent from './layout'
187+
// MyComponent/index.tsx
206188
207-
function MyComponentContainer() {
189+
function MyComponent() {
208190
// Do smart stuff
209191
210192
return <MyComponent />
211193
}
212-
213-
// MyComponent/layout.js
214-
function MyComponent() {
215-
return (
216-
// Some JSX
217-
)
218-
}
219194
```
220195
221196
- **Higher-order Component Naming**: Use a composite of the higher-order component's name and the passed-in component's name as the `displayName` on the generated component. For example, the higher-order component `withFoo()`, when passed a component `Bar` should produce a component with a `displayName` of `withFoo(Bar)`.
@@ -270,15 +245,15 @@ src
270245

271246
```jsx
272247
// bad
273-
/* routes.js */
248+
/* routes.ts */
274249
const userListRoute = '/users';
275250
const itemListRoute = '/items';
276251
277252
/* Another file */
278253
import * as Routes from './routes';
279254
280255
// good
281-
/* routes.js */
256+
/* routes.ts */
282257
const Routes = {
283258
userListRoute: '/users',
284259
itemListRoute: '/items'
@@ -405,25 +380,20 @@ src
405380
- Always use object destructuring to explicitly get props variables in the render function of class Components:
406381

407382
```jsx
408-
import MyComponent from './layout';
383+
import Child from './components/Child';
409384

410385
// bad
411-
class MyComponentContainer extends Component {
412-
render() {
413-
return <MyComponent foo={this.props.foo} bar={this.props.bar} />
414-
}
386+
function MyComponent(props) {
387+
return <Child foo={props.foo} bar={props.bar} />
415388
}
416389

417390
// good
418-
class MyComponentContainer extends Component {
419-
render() {
420-
const { foo, bar } = this.props;
421-
return <MyComponent foo={foo} bar={bar} />
422-
}
391+
function MyComponent({ foo, bar }) {
392+
return <Child foo={foo} bar={bar} />
423393
}
424394
```
425395
426-
- Always use object destructuring to explicitly get props variables in layout Components:
396+
- Always use object destructuring to explicitly get props variables:
427397
428398
```jsx
429399
// bad
@@ -472,27 +442,40 @@ src
472442
/>
473443
```
474444
475-
- Avoid passing arrow functions in props when possible. Instead, create a reference to the function and pass that reference.
445+
- Only use inline arrow functions when the arrow function is simple
446+
- If the function is complex, define it as a const beforehand. If you are passing it to a child component. you may use `useCallback` for memoization as well.
476447
477448
> Why? Passing arrow functions as props in render creates a new function each time the component renders, which is less performant.
478449
479450
```jsx
480-
import MyComponent from './layout';
481-
482451
// bad
483-
class MyComponentContainer extends Component {
484-
render() {
485-
return <MyComponent foo={bar => bar + 1} />
486-
}
452+
function MyComponent() {
453+
return <button onClick={event => {
454+
let baz = "Hello World";
455+
console.log(event.target.value + baz)
456+
}} />
487457
}
488458

489459
// good
490-
class MyComponentContainer extends Component {
491-
foo = bar => bar + 1;
460+
function MyComponent() {
461+
const foo = event => {
462+
let baz = "Hello World";
463+
console.log(event.target.value + baz)
464+
};
492465

493-
render() {
494-
return <MyComponent foo={this.foo} />
495-
}
466+
return <button onClick={foo} />
467+
}
468+
469+
// better when passing it to a child component
470+
import Child from './components/Child';
471+
472+
function MyComponent() {
473+
const foo = useCallback(event => {
474+
let baz = "Hello World";
475+
console.log(event.target.value + baz)
476+
}, []);
477+
478+
return <Child onClick={foo} />
496479
}
497480
```
498481
@@ -667,26 +650,20 @@ src
667650
668651
```jsx
669652
// bad
670-
render() {
671-
return <MyComponent variant="long body" foo="bar">
672-
<MyChild />
673-
</MyComponent>;
674-
}
653+
return <MyComponent variant="long body" foo="bar">
654+
<MyChild />
655+
</MyComponent>;
675656

676657
// good
677-
render() {
678-
return (
679-
<MyComponent variant="long body" foo="bar">
680-
<MyChild />
681-
</MyComponent>
682-
);
683-
}
658+
return (
659+
<MyComponent variant="long body" foo="bar">
660+
<MyChild />
661+
</MyComponent>
662+
);
684663

685664
// good, when single line
686-
render() {
687-
const body = <div>hello</div>;
688-
return <MyComponent>{body}</MyComponent>;
689-
}
665+
const body = <div>hello</div>;
666+
return <MyComponent>{body}</MyComponent>;
690667
```
691668
692669
## Tags

0 commit comments

Comments
 (0)