liveBook · Manning

Class component

As explained in React Hooks in Action: With Suspense and Concurrent Mode

A class component is a React component defined as a JavaScript class extending React's component base or compatible class behavior.

A class component is a JavaScript class extending React.Component or React.PureComponent. Its constructor can initialize state, lifecycle methods can respond to updates or perform side effects, and render returns the UI. section 1.1.3

Use the pattern to organize component behavior around:

class MyComponent extends React.Component {
  constructor (props) {
    super(props);
 
    this.state = {
      // Set up state here.                         #1
    };
  }
 
  componentDidMount () {
    // Perform a side effect like loading data.     #2
  }
 
  render () {
    // Return the UI using prop values and state.   #3
  }
}

Use the listing to see the constructor, componentDidMount, and render methods arranged in one class component. section 1.1.3

How React Hooks in Action: With Suspense and Concurrent Mode covers it

How deep it goes
Defines constructors, lifecycle methods, and render, but defers detailed lifecycle mechanics to comparison with Hooks.
Examples
Shows a class listing with constructor state, componentDidMount data loading, and render-generated UI.
What it assumes
Assumes JavaScript classes, React components, props, and JSX, while explaining the class-based stateful pattern.
What you can do after
Build it from scratch as a stateful React component with constructor state, lifecycle side effects, and render-based UI.
How it connects
Connects class components with props, state, render, lifecycle methods, side effects, and React Hooks.

Also explained in

Compare how 2 books explain class component →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of React Hooks in Action: With Suspense and Concurrent Mode. The section links point to the parts of the book it draws on.