Other Standards
- Basic Rules
- Class vs
React.createClass
vs stateless - Mixins
- Naming
- Declaration
- Alignment
- Quotes
- Spacing
- Props
- Refs
- Parentheses
- Tags
- Methods
- Ordering
isMounted
- Redux
- Only include one React component per file.
- However, multiple Stateless, or Pure, Components are allowed per file.
- Always use JSX/TSX syntax.
- Do not use
React.createElement
unless you're initializing the app from a file that is not JSX.
-
If you have internal state and/or refs, prefer
class extends React.Component
overReact.createClass
.// bad const Listing = React.createClass({ // ... render(): JSX.Element { return <div>{this.state.hello}</div>; } }); // good class Listing extends React.Component { // ... render(): JSX.Element { return <div>{this.state.hello}</div>; } }
And if you don't have state or refs, use arrow functions over classes:
// bad class Listing extends React.Component<Props, void> { render(): JSX.Element { return <div>{this.props.hello}</div>; } } // good const Listing = (props: Props) => ( <div>{props.hello}</div> );
Why? Mixins introduce implicit dependencies, cause name clashes, and cause snowballing complexity. Most use cases for mixins can be accomplished in better ways via components, higher-order components, or utility modules.
-
Extensions: Use
.tsx
extension for React components. -
Filename: Use PascalCase for filenames. E.g.,
ReservationCard.tsx
. -
Reference Naming: Use PascalCase for React components and camelCase for their instances.
// bad const ReservationItem = <ReservationCard />; // good const reservationItem = <ReservationCard />;
-
Component Naming: Use the filename as the component name. For example,
ReservationCard.tsx
should have a reference name ofReservationCard
.// bad import { Footer } from './Footer/index'; // good import { Footer } from './Footer/Footer';
-
Props Naming: Avoid using DOM component prop names for different purposes.
Why? People expect props like
style
andclassName
to mean one specific thing. Varying this API for a subset of your app makes the code less readable and less maintainable, and may cause bugs.// bad <MyComponent style="fancy" /> // good <MyComponent variant="fancy" />
-
Do not use
displayName
for naming components. Instead, name the component by reference.// bad export React.createClass({ displayName: 'ReservationCard', // stuff goes here }); // good export class ReservationCard extends React.Component<Props, void> { }
-
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" > <Quux /> </Foo>
-
Always use single quotes (
'
) for JSX attributes, and all other JS.Why? Using the same quote style everywhere is more readable and less dangerous.
// bad <Foo bar="bar" /> // good <Foo bar='bar' /> // bad <Foo style={{ left: "20px" }} /> // good <Foo style={{ left: '20px' }} />
-
Always include a single space in your self-closing tag.
// bad <Foo/> // very bad <Foo /> // bad <Foo /> // good <Foo />
-
Do not pad JSX curly braces with spaces.
// bad <Foo bar={ baz } /> // good <Foo bar={baz} />
-
Always use camelCase for prop names.
// bad <Foo UserName='hello' phone_number={12345678} /> // good <Foo userName='hello' phoneNumber={12345678} />
-
Omit the value of the prop when it is explicitly
true
.// bad <Foo hidden={true} /> // good <Foo hidden />
-
Always include an
alt
prop on<img>
tags. If the image is presentational,alt
can be an empty string or the<img>
must haverole='presentation'
.// bad <img src='hello.jpg' /> // good <img src='hello.jpg' alt='Me waving hello' /> // good <img src='hello.jpg' alt='' /> // good <img src='hello.jpg' role='presentation' />
-
Do not use words like 'image', 'photo', or 'picture' in
<img>
alt
props.Why? Screenreaders already announce
img
elements as images, so there is no need to include this information in the alt text.// bad <img src='hello.jpg' alt='Picture of me waving hello' /> // good <img src='hello.jpg' alt='Me waving hello' />
-
Use only valid, non-abstract ARIA roles.
// bad - not an ARIA role <div role='datepicker' /> // bad - abstract ARIA role <div role='range' /> // good <div role='button' />
-
Do not use
accessKey
on elements.
Why? Inconsistencies between keyboard shortcuts and keyboard commands used by people using screenreaders and keyboards complicate accessibility.
// bad
<div accessKey='h' />
// good
<div />
- Avoid using an array index as
key
prop, prefer a unique ID. (why?)
// bad
{todos.map((todo: Todo, index: number): JSX.Element =>
<Todo
{...todo}
key={index}
/>
)}
// good
{todos.map((todo: Todo): JSX.Element => (
<Todo
{...todo}
key={todo.id}
/>
))}
- Always define explicit defaultProps for all non-required props.
Why? propTypes are a form of documentation, and providing defaultProps means the reader of your code doesn’t have to assume as much. In addition, it can mean that your code can omit certain type checks.
-
Always use ref callbacks.
// bad <Foo ref='myRef' /> // good <Foo ref={(ref) => { this.myRef = ref; }} />
-
Wrap JSX tags in parentheses when they span more than one line.
// bad render(): JSX.Element { return <MyComponent className='long body' foo='bar'> <MyChild /> </MyComponent>; } // good render(): JSX.Element { return ( <MyComponent className='long body' foo='bar'> <MyChild /> </MyComponent> ); } // good, when single line render(): JSX.Element { const body = <div>hello</div>; return <MyComponent>{body}</MyComponent>; }
-
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' />
- Use arrow functions to close over local variables.
Why? It creates a new function on each render, which is desirable.
```typescript
const ItemList = (props: Props): JSX.Element => {
return (
<ul>
{props.items.map((item, index) => (
<Item
key={item.key}
onClick={() => doSomethingWith(item.name, index)}
/>
))}
</ul>
);
}
```
-
Use arrow functions for the render method in the constructor.
// bad class extends React.Component<Props, void> { private onClickDiv() { // do stuff } render(): JSX.Element { return <div onClick={this.onClickDiv} />; } } // good class extends React.Component<Props, void> { private onClickDiv() { // do stuff } render(): JSX.Element { return <div onClick={() => this.onClickDiv()} />; } }
-
Do not use underscore prefix for internal methods of a React component.
Why? Underscore prefixes are sometimes used as a convention in other languages to denote privacy. But, unlike those languages, TypeScript supports the
private
keyword to design an entity as private.// bad React.createClass({ _onClickSubmit() { // do stuff }, // other stuff }); // good class extends React.Component<Props, void> { private onClickSubmit() { // do stuff } // other stuff }
-
Be sure to return a value in your
render
methods.// bad render(): JSX.Element { (<div />); } // good render(): JSX.Element { return (<div />); }
- Ordering for
class extends React.Component
:
- optional
static
methods constructor
getChildContext
componentWillMount
componentDidMount
componentWillReceiveProps
shouldComponentUpdate
componentWillUpdate
componentDidUpdate
componentWillUnmount
- clickHandlers or eventHandlers like
onClickSubmit()
oronChangeDescription()
- getter methods for
render
likegetSelectReason()
orgetFooterContent()
- optional render methods like
renderNavigation()
orrenderProfilePicture()
render
- Ordering for
React.createClass
:
displayName
propTypes
contextTypes
childContextTypes
mixins
statics
defaultProps
getDefaultProps
getInitialState
getChildContext
componentWillMount
componentDidMount
componentWillReceiveProps
shouldComponentUpdate
componentWillUpdate
componentDidUpdate
componentWillUnmount
- clickHandlers or eventHandlers like
onClickSubmit()
oronChangeDescription()
- getter methods for
render
likegetSelectReason()
orgetFooterContent()
- optional render methods like
renderNavigation()
orrenderProfilePicture()
render
- Do not use
isMounted
.
Why?
isMounted
is an anti-pattern, is not available when using ES6 classes, and is on its way to being officially deprecated.
-
Seperate components from their actions and reducers. Thus, you should have one file for your actions, one for your reducers, and one for your component.
-
The file name for your actions should be the name of the component + the suffix
Actions
. -
The file name for your reducers should be the name of the component + the suffix
Reducers
.