Class Component Structure: extends React.Component
What you'll learn: How React components were written before hooks—using ES6 classes that extend React.Component.
The Basic Syntax
Before function components and hooks became the standard, React components were written as JavaScript classes. To create a class component, you use the extends keyword to inherit from React.Component:
class Welcome extends React.Component {
render() {
return <h1>Hello, World!</h1>;
}
}
Breaking Down the Pattern
Think of React.Component as a blueprint that contains all the React magic. When you write extends React.Component, you're saying "create a new component type that inherits all of React's built-in functionality."
Every class component must have a render() method that returns JSX—this is where you define what appears on screen, just like the return statement in function components.
The Constructor Pattern
Class components often include a constructor for initialization:
class Counter extends React.Component {
constructor(props) {
super(props);
// Initialization happens here
}
render() {
return <div>Counter Component</div>;
}
}
The super(props) call is mandatory—it passes props to the parent React.Component class so React can handle them internally.
Why You're Learning This
While hooks (introduced much later) replaced most class component use cases, you'll encounter class components in legacy codebases, older tutorials, and existing libraries. Understanding this syntax helps you read and maintain existing code.
Key Takeaway: Class components use extends React.Component to inherit React's functionality and require a render() method to return JSX—this was the standard pattern before hooks revolutionized React development.