Legacy component maintenance
Maintaining an existing class-based React component.
React 19+ · Class
React.Component is the base class for class-based React components. Extend it and implement render; however, function components are recommended for new code.
React.Component is the base class for defining class-based React components. Extend it, implement render, and use props, state, or context through the instance; React calls render when it needs to determine the displayed UI. New code should generally prefer function components.
class MyComponent extends Component<Props, State> { render() { return ...; } }A React class component instance whose render method produces React nodes.
import { Component } from 'react';
class Greeting extends Component<{ name: string }> {
render() {
return <h1>Hello, {this.props.name}!</h1>;
}
}
export default function App() {
return <Greeting name="Ada" />;
}This class component extends React.Component and renders JSX from its props.
Maintaining an existing class-based React component.
Using lifecycle methods or instance state in a class architecture.
Consuming a declared context from a class instance.
Extend Component and define a render method that returns the JSX React should display. React invokes render whenever it needs to determine the component's output.
Follow-up: How should state be changed? Use setState rather than directly mutating this.state.
Store state in the component's state field, which must be an object. Call setState to request an update instead of changing the field directly.
Follow-up: What constraint applies to render? It should be pure and only calculate JSX.
A class component receives context through this.context only when it declares which context to consume with static contextType. The consumed context is then available on the instance.
Follow-up: What is the recommended alternative for new components? Prefer function components rather than class components.
ComponentPureComponentcreateElementmemo