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.
import { Component } from 'react'; class Greeting extends Component { render() { return <h1>Hello, {this.props.name}!</h1>; } }
Try it
Syntax
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 leavesthis.propsundefined inside the constructor. With class fields you rarely need a constructor. - Bind or use arrow fields. A method passed as
onClick={this.like}losesthisunless 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.stateorthis.props. Always create new objects and arrays. - Only one context.
contextTypereads a single context. To read several, use<Context.Consumer>or convert to a function component and useuseContext. - 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.
ComponentvsPureComponent. See PureComponent for the version that skips unneeded renders.- Version note. The old lifecycle names
componentWillMount,componentWillReceivePropsandcomponentWillUpdateare legacy; if present they need anUNSAFE_prefix. Prefer the current methods in Class lifecycle methods.