What is ordering on components in React style guides from Airbnb?
Airbnb has a JavaScript Github repo where they keep a list of the best practices in JavaScript, as well as CSS, React, CSS-in-JavaScript, and more. In this shot, we will be discussing the important rules of writing React.
Most of these rules are based on making our React code clearer and cleaner, and a few of them make our React app run faster. We will look into methods and properties ordering on components.
Methods and properties ordering on components
Methods and properties of React components should be placed in an orderly fashion:
- Optional static methods
constructorgetChildContextcomponentWillMountcomponentDidMountcomponentWillReceivePropsshouldComponentUpdatecomponentWillUpdatecomponentDidUpdatecomponentWillUnmount- Click handlers or event handlers like
onClickSubmit()oronChangeDescription() - Getter methods for render like
getSelectReason()orgetFooterContent() - Optional render methods like
renderNavigation()orrenderProfilePicture() - Render
The list above shows how methods and properties should be placed in a React component. Notice that from componentWillMount to componentWillUnmount, they are arranged in the order they will be called in a component lifecycle.
The static methods come first:
class MyComponent extends React.Component {
static aStaticMethod() {
// ...
}
}
Then, the constructor method and getChildContext:
class MyComponent extends React.Component {static aStaticMethod() {// ...}constructor() {// ...}getChildContext() {// ...}}
Next come the lifecycle hooks:
class MyComponent extends React.Component {static aStaticMethod() {// ...}constructor() {// ...}getChildContext() {// ...}componentWillMount() {// ...}componentDidMount() {// ...}componentWillReceiveProps() {// ...}shouldComponentUpdate() {// ...}componentWillUpdate() {// ...}componentDidUpdate() {// ...}componentWillUnmount() {// ...}}
Next, the event handlers:
class MyComponent extends React.Component {static aStaticMethod() {// ...}constructor() {// ...}getChildContext() {// ...}componentWillMount() {// ...}componentDidMount() {// ...}componentWillReceiveProps() {// ...}shouldComponentUpdate() {// ...}componentWillUpdate() {// ...}componentDidUpdate() {// ...}componentWillUnmount() {// ...}onSubmitHandler() {// ...}clickHandler() {// ...}}
Next, we add any getter methods.
Finally, we add the render method:
class MyComponent extends React.Component {static aStaticMethod() {// ...}constructor() {// ...}getChildContext() {// ...}componentWillMount() {// ...}componentDidMount() {// ...}componentWillReceiveProps() {// ...}shouldComponentUpdate() {// ...}componentWillUpdate() {// ...}componentDidUpdate() {// ...}componentWillUnmount() {// ...}onSubmitHandler() {// ...}clickHandler() {// ...}render() {// ...}}
React static properties are then arranged as follows:
propTypesdefaultPropscontextTypes- etc.
class MyComponent extends React.Component {
// ...
}
MyComponent.propTypes = propTypes;
MyComponent.defaultProps = defaultProps;
MyComponent.contextTypes = contextTypes;