When to use a Class Component over a Function Component in ReactJS ? - GeeksforGeeks (2024)

Last Updated : 31 May, 2023

Summarize

Comments

Improve

Introduction: Components are the building blocks of creating user interfaces in React. They allow us to develop re-usable and independent bits of inter-linked components which together form the whole web interface. React has 2 different types of components:

Syntax:

Class Component – The components created using the ES2015 class template are known as class components.

class FirstComponent extends React.Component { render() { return <p>This is a class component!</p>; }}

Function Component – The components created using the JavaScript function syntax are functional components.

function anotherComponent(props) { return <p>This is a function component!</p>;}

Now that we know what components are, let’s try to understand when should we use class or function components. We need to understand the concept of the state of the component first. You can refer to this article for a better understanding of React state.

In the case of class components, when the state of the component is changed then the render method is called. Whereas, function components render the interface whenever the props are changed.

Although we should prefer using function components most times as React also allows using state with function components in React version 16.8 and later by using the useState hook, which is also recommended by Meta (Facebook App) itself. But for the older react versions, we can use class components where we have a dependency on the state of the components.

Class Components vs Function Components:

Class Components

Function Components

Class components need to extend the component from “React.Component” and create a render function that returns the required element.Functional components are like normal functions which take “props” as the argument and return the required element.
They are also known as stateful components.They are also known as stateless components.
They implement logic and the state of the component.They accept some kind of data and display it in the UI.
Lifecycle methods can be used inside them.Lifecycle methods cannot be used inside them.

Format of Hooks inside class components:

constructor(props) { super(props); this.state = { color: ‘ ‘ }}

Format of Hooks inside function components:

const [color,SetColor]= React.useState('')
It needs to store state therefore constructors are used.Constructors are not used in it.
It has to have a “render()” method inside that.It does not require a render method.

Example 1: This is a basic example that’ll illustrate Class Component and functional component:

Class Component:

Javascript

Output:

When to use a Class Component over a Function Component in ReactJS ? - GeeksforGeeks (1)

Example of Class Component

Function Component:

Javascript

import React from "react";

const FunctionComponent = () => {

return <h1>It's a function component!</h1>;

};

export default FunctionComponent;

Output:

When to use a Class Component over a Function Component in ReactJS ? - GeeksforGeeks (2)

Example of Function Component

Example 2: This is another example of Class Component and functional component:

Class Component:

Javascript

import React from "react";

class Something extends React.Component {

render() {

return <h1>A Computer Science

Portal For Geeks</h1>;

}

}

class ClassComponent extends React.Component {

render() {

return <Something />;

}

}

export default ClassComponent;

Output:

When to use a Class Component over a Function Component in ReactJS ? - GeeksforGeeks (3)

Example of Class Component

Function Component:

Javascript

import React from "react";

const Something = () => {

return <h1>It's a better function

component!</h1>;

};

const FunctionComponent = () => {

return <Something />;

};

export default FunctionComponent;

Output:

When to use a Class Component over a Function Component in ReactJS ? - GeeksforGeeks (4)

Example of Function Component



When to use a Class Component over a Function Component in ReactJS ? - GeeksforGeeks (5)

Improve

Please Login to comment...

When to use a Class Component over a Function Component in ReactJS ? - GeeksforGeeks (2024)
Top Articles
What is a Mutual Fund ? - Insight - Blog Site of BanyanFA LLP
Rebalance Your Asset Allocation Guide and Best Portfolio Rebalancing Strategies
Mickey Moniak Walk Up Song
Using GPT for translation: How to get the best outcomes
Skycurve Replacement Mat
Metallica - Blackened Lyrics Meaning
Beacon Schnider
America Cuevas Desnuda
5 Bijwerkingen van zwemmen in een zwembad met te veel chloor - Bereik uw gezondheidsdoelen met praktische hulpmiddelen voor eten en fitness, deskundige bronnen en een betrokken gemeenschap.
South Carolina defeats Caitlin Clark and Iowa to win national championship and complete perfect season
How to Watch Braves vs. Dodgers: TV Channel & Live Stream - September 15
Vichatter Gifs
Caliber Collision Burnsville
Wisconsin Women's Volleyball Team Leaked Pictures
Cvb Location Code Lookup
Suffix With Pent Crossword Clue
Char-Em Isd
Q33 Bus Schedule Pdf
Fdny Business
Second Chance Maryland Lottery
Hellraiser III [1996] [R] - 5.8.6 | Parents' Guide & Review | Kids-In-Mind.com
List of all the Castle's Secret Stars - Super Mario 64 Guide - IGN
U Arizona Phonebook
Ruben van Bommel: diepgang en doelgerichtheid als wapens, maar (nog) te weinig rendement
Sodium azide 1% in aqueous solution
Magic Seaweed Daytona
Seeking Arrangements Boston
Xfinity Cup Race Today
Craigslist Maryland Trucks - By Owner
Regal Amc Near Me
Globle Answer March 1 2023
Koninklijk Theater Tuschinski
Craig Woolard Net Worth
Nk 1399
What Sells at Flea Markets: 20 Profitable Items
Tinyzonehd
A Man Called Otto Showtimes Near Carolina Mall Cinema
Craigs List Jax Fl
WOODSTOCK CELEBRATES 50 YEARS WITH COMPREHENSIVE 38-CD DELUXE BOXED SET | Rhino
What are the 7 Types of Communication with Examples
Taktube Irani
The Rise of "t33n leaks": Understanding the Impact and Implications - The Digital Weekly
Was heißt AMK? » Bedeutung und Herkunft des Ausdrucks
Blackstone Launchpad Ucf
Academic important dates - University of Victoria
Mississippi weather man flees studio during tornado - video
Tgirls Philly
Craigslist Rooms For Rent In San Fernando Valley
A rough Sunday for some of the NFL's best teams in 2023 led to the three biggest upsets: Analysis
Nfsd Web Portal
Costco Gas Price Fort Lauderdale
Latest Posts
Article information

Author: Lakeisha Bayer VM

Last Updated:

Views: 6461

Rating: 4.9 / 5 (49 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Lakeisha Bayer VM

Birthday: 1997-10-17

Address: Suite 835 34136 Adrian Mountains, Floydton, UT 81036

Phone: +3571527672278

Job: Manufacturing Agent

Hobby: Skimboarding, Photography, Roller skating, Knife making, Paintball, Embroidery, Gunsmithing

Introduction: My name is Lakeisha Bayer VM, I am a brainy, kind, enchanting, healthy, lovely, clean, witty person who loves writing and wants to share my knowledge and understanding with you.