React ES6 Classes

React ES6 Classes – Complete Guide with Examples
Before React Hooks became popular, ES6 class components were the main way to create React components. Even today, They are an important topic for understanding legacy code, interviews, and React fundamentals.
In this guide, you’ll learn what They are, how React class components work, syntax with examples, and why functional components replaced them.
What Are ES6 Classes?
ES6 Classes are a modern way to create objects and handle inheritance in JavaScript. React uses ES6 classes to create class-based components.
Basic ES6 Class Example
- Introduced in ES6
- Cleaner than prototype-based syntax
- Supports inheritance using
extends
React Class Components Explained
A React class component is created using an ES6 class that extends React.Component.
Basic React ES6 Class Component
Key Rules
Must extend
React.ComponentMust include a
render()methodrender()returns JSX
Why React Used ES6 Classes?
Before Hooks, React used classes because they allowed:
- State management
- Lifecycle methods
- Better structure than plain functions
However, class components are now less common in new projects.
Constructor in React ES6 Classes
The constructor() is used to initialize state and bind methods.
Example
super(props)is mandatory before usingthis.
State in React ES6 Class Components
State is stored inside this.state.
Updating State
Complete Example
- Never modify state directly
- Always use
setState()
Props in ES6 Class Components
Props are accessed using this.props.
Usage:
- Props are read-only
- Passed from parent component
Event Handling in Class Components
Event handlers must be bound to this.
Example
- Arrow functions automatically bind
this.
Lifecycle Methods in React ES6 Classes
Lifecycle methods allow you to control component behavior.
Common Lifecycle Methods
| Method | Purpose |
|---|---|
| componentDidMount | Runs after component loads |
| componentDidUpdate | Runs after update |
| componentWillUnmount | Cleanup before removal |
Example
React ES6 Classes vs Functional Components
| Feature | Class Component | Functional Component |
|---|---|---|
| Syntax | Complex | Simple |
| State | this.state | useState |
| Lifecycle | Lifecycle methods | useEffect |
| Performance | Slightly slower | Faster |
| Modern usage | Rare | Recommended |
Hooks replaced most use cases of class components
When Should You Use React ES6 Classes?
- Maintaining old React projects
- Reading legacy code
- Interview preparation
- Understanding React internals
Not recommended for new projects
Common Mistakes
- Forgetting
super(props) Directly modifying state- Not binding event handlers
- Writing logic inside
render() Overusing lifecycle methods
Interview Questions
1. What is a React class component?
A component created using ES6 class syntax that extends React.Component.
2. Why use super(props)?
To initialize the parent React.Component and access this.props.
3. How is state updated in class components?
Using this.setState().
4. Are class components still used?
Yes, mainly in legacy projects.
5. What replaced class components?
Functional components with Hooks.
Best Practices for React ES6 Classes
- Keep components small
- Avoid heavy logic in render
- Use arrow functions for handlers
- Move to functional components when possible
Summary
- They were core to early React
- Use
extends React.Component State and lifecycle methods handled inside class- Still important for interviews & legacy apps
- Hooks are the modern replacement
Understanding They helps you become a better React developer, especially when working with older codebases.
