React components are the building blocks of any React application. There are two main types: Functional Components and Class Components. Understanding the differences between them is key to writing modern, efficient React applications.
What are Functional Components?
Functional components are JavaScript functions that return JSX. They are simpler, easier to read, and preferred in modern React development.
✅ Key Features:
- Simpler syntax – Just a function returning JSX.
- Hooks support – Can use
useState
,useEffect
, etc. - Better performance – No overhead from class instantiation.
Example of a Functional Component
import { useState } from "react";
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<p>{count}</p>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
}
What are Class Components?
Class components are ES6 classes that extend React.Component
. Before hooks were introduced, they were the primary way to manage state and lifecycle methods.
⚠️ Key Features:
- Uses
this.state
andthis.setState
for state management. - Lifecycle methods like
componentDidMount
andcomponentDidUpdate
. - More boilerplate compared to functional components.
Example of a Class Component
import React, { Component } from "react";
class Counter extends Component {
constructor(props) {
super(props);
this.state = { count: 0 };
}
increment = () => {
this.setState({ count: this.state.count + 1 });
};
render() {
return (
<div>
<p>{this.state.count}</p>
<button onClick={this.increment}>Increment</button>
</div>
);
}
}
Functional vs. Class Components: Key Differences
Feature | Functional Components | Class Components |
---|---|---|
Syntax | Simple function | ES6 class |
State Management | useState Hook | this.state & setState |
Lifecycle Methods | useEffect Hook | componentDidMount , etc. |
Performance | More efficient | Slightly slower due to class overhead |
Code Complexity | Less boilerplate | More setup required |
Why Functional Components Are Preferred
Modern React development favors functional components because they:
✅ Require less code
✅ Improve performance
✅ Support hooks for better state management
✅ Are easier to read and test
With the introduction of React Hooks (React 16.8), almost everything class components could do is now possible in functional components—making class components largely obsolete for new projects.
Summary
- Functional components are the modern, recommended way to write React components.
- Class components were used before hooks but are now less common.
- Hooks like
useState
anduseEffect
make functional components powerful and easy to use.
References
- React Official Docs – Components
- Understanding React Functional Components
- Class Components vs. Functional Components