React Components: Function-Based vs Class-Based
Choosing function based components or class based components is a common question a lot of us face when starting your React journey. In this blog, we will talk about both of these approaches and guide you along with examples.
Function-Based Components
In programming, we make functions to reuse the code and make it more readable. A function is just a block of code given some label, and we can execute that block of code by using that label i.e function name.
Our web applications are simply made of HTML, CSS and JavaScript. And in React we simply breakdown our apps into smaller parts called components. So, components are merely the HTML, CSS, JS chunks.
Example:
import React from 'react';
const FunctionComponent = () => {
return <p>Hello, Function Component!</p>;
};
Features:
Simplicity: Functions are concise, making the code easy to read and maintain.
No
this
Keyword: Eliminates confusion around thethis
keyword, simplifying the component structure.
Class-Based Components
Class-based components have been a traditional way of defining React components, incorporating the concepts of ES6 classes.
Example:
import React, { Component } from 'react';
class ClassComponent extends Component {
render() {
return <p>Hello, Class Component!</p>;
}
}
Features:
Lifecycle Methods: Enables the use of lifecycle methods like
componentDidMount
,componentDidUpdate
, etc.State Management: Supports local state management using
this.state
andthis.setState()
.
When to Use Function-Based Components?
Functional Paradigm: When adhering to a functional programming paradigm or following a more functional approach.
Simplicity: For simpler components without the need for state or lifecycle methods.
Hooks: When utilizing React Hooks for state and side effects.
Example: Function-Based Component with Hooks
import React, { useState, useEffect } from 'react';
const FunctionWithHooks = () => {
const [count, setCount] = useState(0);
useEffect(() => {
document.title = `Count: ${count}`;
}, [count]);
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
};
When to Use Class-Based Components?
Legacy Codebases: When working with older codebases that extensively use class components.
Lifecycle Methods: For components requiring lifecycle methods for tasks like data fetching or DOM manipulation.
Local State: When managing local state using
this.state
is necessary.
Example: Class-Based Component with State
import React, { Component } from 'react';
class ClassWithState extends Component {
constructor(props) {
super(props);
this.state = {
count: 0,
};
}
componentDidMount() {
document.title = `Count: ${this.state.count}`;
}
componentDidUpdate() {
document.title = `Count: ${this.state.count}`;
}
render() {
return (
<div>
<p>Count: {this.state.count}</p>
<button onClick={() => this.setState({ count: this.state.count + 1 })}>
Increment
</button>
</div>
);
}
}
Conclusion
Function-based components and class-based components are both essential to React development, providing different approaches for doing the same thing.
The introduction of hooks has allowed function components to handle state and side effects, blurring the distinction between the two techniques.
So, you will find old code written using class based syntax and new with function based syntax.
You should understand the differences between each so that you can work on projects or third party packages that use either or both of the two approaches.
Thanks for Reading!
Subscribe to my newsletter
Read articles from Piyush Nanwani directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Piyush Nanwani
Piyush Nanwani
Experienced full-stack developer with a passion for tackling complex challenges. Dedicated to helping you become a top 1% mobile app developer. Passionate about mentoring, teaching, and continuous learning in new technologies.