Skip to content
Tutorials Center
My learning

Topics

React Reference ยท Components and Special Elements

React Component class

Reference for React.Component, the base class of class components: this.props, this.state, render(), setState(), forceUpdate(), contextType and static fields.

Component is the base class for components written as JavaScript classes. You extend it, give your class a render() method that returns JSX, and React creates one instance per mounted component. The instance carries this.props, this.state, and the methods that change them.

Class components are not deprecated and still work in React 19, but new code should be written as function components with hooks. You meet classes in older code and in one place that still requires them: error boundaries.

JSX
import { Component } from 'react';

class Greeting extends Component {
  render() {
    return <h1>Hello, {this.props.name}!</h1>;
  }
}

Try it

Props, state and a method
contextType and forceUpdate

Syntax

JSX
class MyComponent extends Component {
  constructor(props) { super(props); }
  state = {};
  static contextType = SomeContext;
  render() { return <div />; }
}

Instance members that React provides:

Member Description
this.props The props passed by the parent. Read-only.
this.state The component's state object. Read it freely; change it only through setState.
this.context The value of the context named in static contextType. undefined without it.
this.setState(update, callback?) Schedules a state change and a re-render. See this.state and setState.
this.forceUpdate(callback?) Re-renders even if shouldComponentUpdate would say no.
render() Required. Returns what to display. Must be pure: no state changes, no side effects.

Static fields: contextType (one context), defaultProps (still honored on classes), and getDerivedStateFromProps / getDerivedStateFromError.

Errors: a class with no render method throws. Calling setState in render or in the constructor causes warnings or loops. Calling this.setState on an unmounted instance does nothing and, in some older versions, warned.

Usage notes

  • Call super(props) first if you write a constructor. Skipping it leaves this.props undefined inside the constructor. With class fields you rarely need a constructor.
  • Bind or use arrow fields. A method passed as onClick={this.like} loses this unless it is an arrow function field or bound in the constructor.
  • State is merged shallowly by setState; hooks' setters replace instead. Details in this.state and setState.
  • Do not mutate this.state or this.props. Always create new objects and arrays.
  • Only one context. contextType reads a single context. To read several, use <Context.Consumer> or convert to a function component and use useContext.
  • No hooks. Hooks work only in function components. A class cannot call useState.
  • Inheritance is discouraged. Build with composition and props rather than subclassing your own components.
  • Component vs PureComponent. See PureComponent for the version that skips unneeded renders.
  • Version note. The old lifecycle names componentWillMount, componentWillReceiveProps and componentWillUpdate are legacy; if present they need an UNSAFE_ prefix. Prefer the current methods in Class lifecycle methods.