Adding First Class Based Component
A class-based component is a React component written as a JavaScript class that extends React.Component and returns its UI from a render() method. Function components with hooks are the usual choice in new code, but class components still appear in many existing projects, and error boundaries can only be written as classes. Building your first one shows how props, state and lifecycle methods were organised before hooks.
The minimum a class component needs
Every class component has two required parts: it must extend Component and it must define render(). The render method returns JSX, just like a function component's return value. Props are available on this.props instead of a function parameter.
import { Component } from 'react';
class Greeting extends Component {
render() {
const { name, role } = this.props;
return (
<div className="greeting">
<h2>Hello, {name}!</h2>
{role && <p>Role: {role}</p>}
</div>
);
}
}
export default Greeting;
// Usage elsewhere:
// <Greeting name="Asha" role="Admin" />
Comparing it with the function version
The same component as a function is shorter: function Greeting({ name, role }) { return ...; }. The class adds ceremony, but it groups everything about the component inside one class body: data in this.state, behaviour in methods, and side effects in lifecycle methods such as componentDidMount. Seeing both versions side by side makes it easier to convert old code to hooks later, or to read a class component someone else wrote.
Adding a constructor and default props
If the component will hold state, you add a constructor. It must call super(props) before using this, otherwise JavaScript throws an error. You can also set default prop values with a static property.
import { Component } from 'react';
class ProfileCard extends Component {
static defaultProps = {
role: 'Member',
};
constructor(props) {
super(props);
this.state = { expanded: false };
}
render() {
const { name, role } = this.props;
return (
<section>
<h3>{name}</h3>
<p>{role}</p>
{this.state.expanded && <p>Extra details go here.</p>}
</section>
);
}
}
export default ProfileCard;
Changing expanded requires this.setState and an event handler, which is exactly what the next lesson on states and events covers.
Mistakes that trip people up
Forgetting this. is the most common one. Inside a class, props.name or state.count is undefined; it must be this.props.name. A close second is writing render as a separate function outside the class or misspelling it as Render, which leaves React with nothing to display and an error about a missing render method.
Another slip is mutating props. Props belong to the parent, so assigning this.props.name = 'x' is not allowed and will not update the screen. Copy the value into state if the component needs its own editable version.
Finally, component names must start with a capital letter. React treats <greeting /> as an unknown HTML tag, not your class.
Where class components still matter
You will meet class components when maintaining older codebases, reading older tutorials and writing error boundaries, which rely on componentDidCatch and static getDerivedStateFromError. For everything else, function components are recommended, but both can live side by side in one app.
Also keep each class focused. Older codebases sometimes contain huge class components with many methods; when you add your own, split the UI into smaller components the same way you would with functions.
Quick recap
- Extend
Componentand return JSX fromrender(). - Read props through
this.props. - Call
super(props)first in any constructor. - Capitalise the class name and export it like any module.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.