- Only include one React component per file.
- Use Functional components
- Always use JSX syntax.
- Do not use
React.createElementunless you're initializing the app from a file that is not JSX.
-
Component Naming: Use the filename as the component name. For example,
ReservationCard.jsxshould have a reference name ofReservationCard. However, for root components of a directory, useindex.jsxas the filename and use the directory name as the component name:// bad import Footer from './Footer/Footer'; // bad import Footer from './Footer/index'; // good import Footer from './Footer';
-
Use PascalCase naming convention for filename as well as component name, e.g. GlobalHeader.js
// Bad // Filename: foo.js const Foo = () => {} export default Foo; // Good // Filename: Foo.js const Foo = () => {} export default Foo;
-
Use PascalCase for type names (Use Intefaces or Types).
-
Do not use "I" as a prefix for interface names.
-
Use PascalCase for enum values.
-
Use camelCase for function names.
-
Use camelCase for property names and local variables.
-
Use whole words in names when possible.
-
Use isXXXing or hasXXXXed for variables representing states of things (e.g. isLoading, hasCompletedOnboarding).
-
Follow these alignment styles for JSX syntax
// bad <Foo superLongParam="bar" anotherSuperLongParam="baz" /> // good <Foo superLongParam="bar" anotherSuperLongParam="baz" /> // if props fit in one line then keep it on the same line <Foo bar="bar" /> // children get indented normally <Foo superLongParam="bar" anotherSuperLongParam="baz" > <Spazz /> </Foo>
-
Always use double quotes (
") for JSX attributes, but single quotes for all other JS.// bad <Foo bar='bar' /> // good <Foo bar="bar" /> // bad <Foo style={{ left: "20px" }} /> // good <Foo style={{ left: '20px' }} />
- Always use camelCase for prop names.
// bad
<Foo
UserName="hello"
phone_number={12345678}
/>
// good
<Foo
userName="hello"
phoneNumber={12345678}
/>-
Always self-close tags that have no children.
// bad <Foo className="stuff"></Foo> // good <Foo className="stuff" />
-
If your component has multi-line properties, close its tag on a new line.
// bad <Foo bar="bar" baz="baz" /> // good <Foo bar="bar" baz="baz" />
For stateless components use the function syntax, introduced in React 0.14.
```javascript
// Using an ES2015 (ES6) arrow function:
var Aquarium = (props) => {
var fish = getFish(props.species);
return <Tank>{fish}</Tank>;
};
// Or with destructuring and an implicit return, simply:
var Aquarium = ({species}) => (
<Tank>
{getFish(species)}
</Tank>
);
// Then use: <Aquarium species="rainbowfish" />
```
-
Name methods using
'handle' + triggering event, e.g.handleClick -
Bind handler using the ES6 arrow syntax, so inside the callback it has always the right context
const Foo = () => { handleClick = (e) => { // action } return <button onClick={handleClick}>Submit</button>; }
// CommentListContainer.js
const CommentListContainer = () => {
const [state, setState] = React.useState();
useEffect(() => {
fetch("url")
.then(response => response.json())
// 4. Setting *dogImage* to the image url that we received from the response above
.then(data => setState(data.message))
},[]):
return <CommentList comments={this.state.comments} />;
}
// CommentList.js
const CommentList = (props: Props) => {
renderComment({body, author}) {
return <li>{body}—{author}</li>;
}
return <ul> {props.comments.map(renderComment)} </ul>;
}Source: https://medium.com/@learnreact/container-components-c0e67432e005
return (
<Foo>
<Bar />
</Foo>
);When rendering a list of components from an array, do it inline if it makes sense. If the map function is too long or complicated, consider extracting it out into its own method on the component class.
return (
<ul>
{this.state.fooList.map(fooItem => <FooItem>{fooItem}</FooItem>)}
</ul>
);Indentation and new line for component/element attributes.
<input
type="text"
value={this.state.foo}
onChange={this._handleInputChange.bind(this, 'foo')}
/>- Static properties should be set in the SCSS, dynamic ones in JS.
.Foo {
background-color: #ff0;
}const Foo = () => {
const styles = {
'transform': 'translateX(' + this.state.position + ' + px)'
};
return <div className="Foo" styles={classes}>Foo Header</div>;
}- Use CSS modules (style files)
- Tailwind CSS classes:
const BUTTON = 'w-24 full-rounded p-4 ....';https://medium.com/@dan_abramov/mixins-are-dead-long-live-higher-order-components-94a0d2f9e750