Class-Based vs Functional Components in React — Key Differences for Interviews

Learn the key differences between class-based and functional components in React, including lifecycle methods, hooks, state management, and interview tips.
This is one of the most common React interview questions, as it directly relates to React lifecycle methods.
React components are the building blocks of a React application, and understanding their differences helps you write efficient, maintainable code.
Components in React
In React, the Document Object Model (DOM) is where all elements are rendered. Manipulating the DOM — whether loading, updating, or removing content — often involves lifecycle methods.
React has three main lifecycle phases:
Mounting – Component is being inserted into the DOM.
Updating – Component is being re-rendered due to changes in state or props.
Unmounting – Component is being removed from the DOM.
I) Class-Based Components
Class-based components can be stateful or stateless, depending on whether they manage internal state.
Key Features
Require a
render()
method to return JSX.Can use both props and state.
Support all lifecycle methods like
componentDidMount()
,componentDidUpdate()
, andcomponentWillUnmount()
.
Common Lifecycle Methods
componentDidMount() — Called once after the initial render.
Example use: Fetching API data on page load.
componentDidUpdate()
— Called after a component updates (state/props change).componentWillUnmount() — Called before the component is removed from the DOM.
Example use: Cleaning up event listeners or timers.
constructor()
— Runs before rendering; useful for initializing state.
JavaScript
class Counter extends React.Component {
constructor() {
super()
this.state = { count: 0 } // Initializing state
}
componentDidMount() {
console.log('Component mounted!')
}
componentDidUpdate() {
console.log('Component updated!')
}
componentWillUnmount() {
console.log('Component will unmount!')
}
render() {
return <h1>{this.state.count}</h1>
}
}
II) Functional Components
Functional components were originally stateless, but since React 16.8, hooks allow them to manage state and handle lifecycle logic.
Key Features
No
render()
method — simply return JSX.Use
useState()
for state anduseEffect()
for lifecycle events.Accept props as arguments.
Using useEffect()
for Lifecycle Events
JavaScript
import React, { useEffect, useState } from 'react'
function Counter({ number }) {
const [count, setCount] = useState(0)
// Mounting
useEffect(() => {
console.log('Functional component: Mounted')
}, [])
// Updating
useEffect(() => {
console.log('Functional component: Updated')
}, [number])
// Unmounting
useEffect(() => {
return () => {
console.log('Functional component: Unmounted')
}
}, [number])
return <h1>{count}</h1>
}
Class vs Functional Components — Quick Comparison
Feature | Class-Based Component | Functional Component |
Syntax | ES6 Class | JavaScript Function |
State |
|
|
Lifecycle Methods |
|
|
Render Method | Required ( | Not required |
Performance | Slightly heavier | Generally lighter and faster |
Hooks Support | ❌ (Not applicable) | ✅ Yes (from React 16.8) |
Code Readability | More verbose | Shorter, more concise |
Interview Tip
If asked in an interview, mention that hooks have largely replaced the need for most class-based components, but class components are still found in many legacy projects.
Show that you understand lifecycle mapping between classes and hooks:
componentDidMount
→useEffect(..., [])
componentDidUpdate
→useEffect(..., [dependency])
componentWillUnmount
→ Cleanup function inuseEffect
📌 Resources:
Why Do Front-End Frameworks Exist?
Modern frameworks like React, Vue, and Angular exist because building complex, interactive apps with just plain JavaScript quickly becomes unmanageable. Let’s look at how web development evolved and why frameworks are the solution.
Read Full StoryWhat is a Function in JavaScript? | Function Examples & Guide
Functions in a JavaScript. Learn what functions are in JavaScript with examples. This beginner-friendly guide covers function declarations, parameters, return values, and more.
Read Full Story📘 TypeScript: Day 2 – Interfaces & Object Types
Let us consider a previous example: const car: { brand: string; year: number } = { brand: "Tata", year: 2025 }; That works fine, but imagine you have lots of objects with the same shape. Typing { brand: string; year: number } again and again is messy. 👉 This is where interfaces come in.
Read Full Story📘 Day 3 – TypeScript: Union Types, Type Aliases & Enums
Unlock the power of **Union Types**, **Type Aliases**, and **Enums** in TypeScript to write cleaner, more flexible code. With **Union Types**, you can let variables hold multiple types, making your code more versatile. **Type Aliases** make complex types easier to read and reuse, while **Enums** help you manage related constants in a more organized way. These features give you the tools to build robust and maintainable applications.
Read Full Story