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
Unauthorised Access
Wallet Money - PayZapp Wallet to Your Account Online
Enrique Espinosa Melendez Obituary
Stadium Seats Near Me
Online Reading Resources for Students & Teachers | Raz-Kids
Boomerang Media Group: Quality Media Solutions
Get train & bus departures - Android
Vaya Timeclock
Wells Fargo Careers Log In
How To Get Free Credits On Smartjailmail
Gameday Red Sox
Lesson 1 Homework 5.5 Answer Key
Weather In Moon Township 10 Days
Missing 2023 Showtimes Near Lucas Cinemas Albertville
Qhc Learning
Hartford Healthcare Employee Tools
Slushy Beer Strain
Trini Sandwich Crossword Clue
O'reilly's Auto Parts Closest To My Location
Gayla Glenn Harris County Texas Update
Beverage Lyons Funeral Home Obituaries
Baldur's Gate 3: Should You Obey Vlaakith?
Yugen Manga Jinx Cap 19
Keyn Car Shows
Dexter Gomovies
4.231 Rounded To The Nearest Hundred
Meggen Nut
Lawrence Ks Police Scanner
Pdx Weather Noaa
The value of R in SI units is _____?
Urban Blight Crossword Clue
Luciipurrrr_
Petsmart Distribution Center Jobs
Microsoftlicentiespecialist.nl - Microcenter - ICT voor het MKB
Afspraak inzien
Hisense Ht5021Kp Manual
Pinellas Fire Active Calls
Myfxbook Historical Data
Raising Canes Franchise Cost
Vision Source: Premier Network of Independent Optometrists
R/Moissanite
60 X 60 Christmas Tablecloths
Panorama Charter Portal
Pickwick Electric Power Outage
Ups Customer Center Locations
The Pretty Kitty Tanglewood
Anonib New
Bama Rush Is Back! Here Are the 15 Most Outrageous Sorority Houses on the Row
Ingersoll Greenwood Funeral Home Obituaries
Provincial Freeman (Toronto and Chatham, ON: Mary Ann Shadd Cary (October 9, 1823 – June 5, 1893)), November 3, 1855, p. 1
Fishing Hook Memorial Tattoo
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.