SlideShare a Scribd company logo
React Class Components vs
functional Components: Which
is Better?
https://fibonalabs.com/
To understand class and functional components, we should first understand
how these to React components evolved. React is an open-source front-end JS
library for building interactive user interfaces. Some of the famous features are
the virtual DOM, one-way data binding, JSX, reusable components, stable
code, fast rendering of UI elements, and performance optimization.
Along with these advantages, React also offers two types of components that
the developers could use to create UI elements. It can be assumed that both
provide the same opportunities for writing UI elements. But it isn't true.
The real reason was that class components were the only option to develop
complex code. The reason was with class components that you get additional
features like state, while functional components didn’t have such an option.
However, the situation changed with React v16.8 which was released in 2019.
It took the development of the functional components to a new level. React
offered hooks for functional components. The introduction of hooks made it
possible to write complex code using only functions.
Functional Components
Props are inputs for both types of components. One of the main tasks of props
is to pass information from one component to another. However, props are
read-only. That means all React components shouldn’t change their props
value and must return the same result. Components that respect their props
are called “pure”. That rule works both for class and function components.
Another way of declaring functional components is by using an arrow function.
Arrow functions have some advantages:
1. It looks compact. Functions are easier to read and write.
Class Components
Props work well with both class and functional components. Additional benefits
of class components are that they have a state and lifecycle. This is the reason
they are called “stateful” components.
The state of a component is an object that holds some information and controls
the behaviour of the component. The difference between props and state is that
props don’t change over time, but the state holds the data that can be changed
over time and changes the component rendering.
Difference between Functional and Class Components
● State and Lifecycle: The difference between functional and class
components was that class components provide features such as
setState() and lifecycle methods componentDidMount(),
componentWillUnmoun(), etc., while functional components don’t.
Functional components are plain JavaScript functions that accept props
and return React elements, while class components are JavaScript classes
that extend React. A component that has a render method. Both state and
lifecycle methods come from React. Component, hence available in only
However, things changed with the introduction of Hooks. To replace setState
(in-class components) React offers useState Hook for functional components.
To work with components lifecycle, classes have methods like
componentDidMount, componentWillUnmount, componentWillUpdate, and
componentDidUpdate whereas functional components have a hook called
useEffect.
● Syntax:
➢ Declaration: Functional components are JavaScript functions whereas
Class components are classes that extend React.Component.
➢ The Way Props are Passes: Working with functional components, we
pass the props as an argument of the function using “props.name”. With
class components, we need to add this. to refer to props.
Functional Component
➢ Handling State: To handle state functional components in React have a
useState()Hook. We assign the initial count equal to 0 and set the method
setCount() that increments by one every time we click a button. The type of
argument can be a number, string, object, or null.
Class components use the setState() function which requires a constructor
and this keyword to refer to the state.
Functional Component
Class Component
➢ Hoisting Works Only for Functional Components: Hoisting is a concept
where variables and function declarations are moved to the top that allows
you to access the variable or function first and then declare it. Javascript
doesn’t modify the code, instead, it puts declarations in memory during the
compilation that allows calling a function before you declare it. Whereas in
classes, if we try to get access to a class before the declaration it throws a
ReferenceError exception.
➢ Running Tests: There are two most popular utilities for testing - Enzyme
and Jest. The enzyme is a JS testing utility for React that allows testing
React renders. Jest is a JS testing framework for creating, running, and
structuring tests.
These two utilities work well with both types of components. There are
some specific things to keep in mind in running tests for functional
components, like, the state hooks are internal to the component and can’t
be tested by calling them. Same with methods and functions.
➢ Performance Difference: People think that functional components have
better performance than class components. React functional components
are simple Objects with 2 properties: type (string) and props (object). To
render such a component React needs to call the function and pass down
the props.
Class components are instances of React.Component with the constructor
in it and complex flows for manipulating state and lifecycle.
Advantages of a Functional Component Over a Class Component
1. A functional component is easier to read and test as they are just plain JS
functions.
2. You write less code.
3. The React team stated that there may be a performance boost for a
functional component in future releases.
In a Nutshell
● Earlier, class components were the only option to add states to
components and manipulate the lifecycle. However, since the introduction
of React Hooks, now we can add the same functionality to function
components.
● The major difference is the syntax. It is related to the way we declare
components, pass props, handle states, and manage the lifecycle.
● Functional components capture the props and state by default.
● Functional components require less code to write. It's subjective and
depends on the developer on how he wants to write the code.
● There are two popular tools to test functional and class components:
Enzyme and Jest.
● There is not much difference in render time between class and functional
components.
THANK YOU

More Related Content

What's hot

Introduction to React JS for beginners
Introduction to React JS for beginners Introduction to React JS for beginners
Introduction to React JS for beginners
Varun Raj
 
React hooks
React hooksReact hooks
React hooks
Ramy ElBasyouni
 
React-JS Component Life-cycle Methods
React-JS Component Life-cycle MethodsReact-JS Component Life-cycle Methods
React-JS Component Life-cycle Methods
ANKUSH CHAVAN
 
Important React Hooks
Important React HooksImportant React Hooks
Important React Hooks
Knoldus Inc.
 
Understanding react hooks
Understanding react hooksUnderstanding react hooks
Understanding react hooks
Maulik Shah
 
React JS & Functional Programming Principles
React JS & Functional Programming PrinciplesReact JS & Functional Programming Principles
React JS & Functional Programming Principles
Andrii Lundiak
 
Full Stack React Workshop [CSSC x GDSC]
Full Stack React Workshop [CSSC x GDSC]Full Stack React Workshop [CSSC x GDSC]
Full Stack React Workshop [CSSC x GDSC]
GDSC UofT Mississauga
 
React js programming concept
React js programming conceptReact js programming concept
React js programming concept
Tariqul islam
 
React hooks Episode #1: An introduction.
React hooks Episode #1: An introduction.React hooks Episode #1: An introduction.
React hooks Episode #1: An introduction.
ManojSatishKumar
 
ReactJs
ReactJsReactJs
Redux workshop
Redux workshopRedux workshop
Redux workshop
Imran Sayed
 
Lets make a better react form
Lets make a better react formLets make a better react form
Lets make a better react form
Yao Nien Chung
 
React Hooks
React HooksReact Hooks
React Hooks
Erez Cohen
 
React js
React jsReact js
React js
Alireza Akbari
 
React js for beginners
React js for beginnersReact js for beginners
React js for beginners
Alessandro Valenti
 
React + Redux Introduction
React + Redux IntroductionReact + Redux Introduction
React + Redux Introduction
Nikolaus Graf
 
Introduction to React JS for beginners | Namespace IT
Introduction to React JS for beginners | Namespace ITIntroduction to React JS for beginners | Namespace IT
Introduction to React JS for beginners | Namespace IT
namespaceit
 
Reactjs
Reactjs Reactjs
Reactjs
Neha Sharma
 
React state
React  stateReact  state
React state
Ducat
 

What's hot (20)

Introduction to React JS for beginners
Introduction to React JS for beginners Introduction to React JS for beginners
Introduction to React JS for beginners
 
React hooks
React hooksReact hooks
React hooks
 
React-JS Component Life-cycle Methods
React-JS Component Life-cycle MethodsReact-JS Component Life-cycle Methods
React-JS Component Life-cycle Methods
 
Important React Hooks
Important React HooksImportant React Hooks
Important React Hooks
 
Understanding react hooks
Understanding react hooksUnderstanding react hooks
Understanding react hooks
 
React JS & Functional Programming Principles
React JS & Functional Programming PrinciplesReact JS & Functional Programming Principles
React JS & Functional Programming Principles
 
Full Stack React Workshop [CSSC x GDSC]
Full Stack React Workshop [CSSC x GDSC]Full Stack React Workshop [CSSC x GDSC]
Full Stack React Workshop [CSSC x GDSC]
 
React js programming concept
React js programming conceptReact js programming concept
React js programming concept
 
React hooks Episode #1: An introduction.
React hooks Episode #1: An introduction.React hooks Episode #1: An introduction.
React hooks Episode #1: An introduction.
 
ReactJs
ReactJsReactJs
ReactJs
 
Redux workshop
Redux workshopRedux workshop
Redux workshop
 
React JS - Introduction
React JS - IntroductionReact JS - Introduction
React JS - Introduction
 
Lets make a better react form
Lets make a better react formLets make a better react form
Lets make a better react form
 
React Hooks
React HooksReact Hooks
React Hooks
 
React js
React jsReact js
React js
 
React js for beginners
React js for beginnersReact js for beginners
React js for beginners
 
React + Redux Introduction
React + Redux IntroductionReact + Redux Introduction
React + Redux Introduction
 
Introduction to React JS for beginners | Namespace IT
Introduction to React JS for beginners | Namespace ITIntroduction to React JS for beginners | Namespace IT
Introduction to React JS for beginners | Namespace IT
 
Reactjs
Reactjs Reactjs
Reactjs
 
React state
React  stateReact  state
React state
 

Similar to React Class Components vs Functional Components: Which is Better?

React
ReactReact
React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...
Karmanjay Verma
 
React JS Interview Question & Answer
React JS Interview Question & AnswerReact JS Interview Question & Answer
React JS Interview Question & Answer
Mildain Solutions
 
Comparison of Function Components and Class Components in React.js
Comparison of Function Components and Class Components in React.jsComparison of Function Components and Class Components in React.js
Comparison of Function Components and Class Components in React.js
StudySection
 
React-JS.pptx
React-JS.pptxReact-JS.pptx
React-JS.pptx
AnmolPandita7
 
Copy of React_JS_Notes.pdf
Copy of React_JS_Notes.pdfCopy of React_JS_Notes.pdf
Copy of React_JS_Notes.pdf
suryanarayana272799
 
React Components.docx
React Components.docxReact Components.docx
React Components.docx
AlishaGupta80
 
REACTJS.pdf
REACTJS.pdfREACTJS.pdf
REACTJS.pdf
ArthyR3
 
Fundamental concepts of react js
Fundamental concepts of react jsFundamental concepts of react js
Fundamental concepts of react js
StephieJohn
 
Introduction to React JS.pptx
Introduction to React JS.pptxIntroduction to React JS.pptx
Introduction to React JS.pptx
SHAIKIRFAN715544
 
What are the components in React?
What are the components in React?What are the components in React?
What are the components in React?
BOSC Tech Labs
 
Choosing Between Functional and Class Components in React: What You Need to Know
Choosing Between Functional and Class Components in React: What You Need to KnowChoosing Between Functional and Class Components in React: What You Need to Know
Choosing Between Functional and Class Components in React: What You Need to Know
StudySection
 
Importance of Hook in Recat Js.pdf
Importance of Hook in Recat Js.pdfImportance of Hook in Recat Js.pdf
Importance of Hook in Recat Js.pdf
Nishaadequateinfosof
 
Introduction to ReactJS UI Web Dev .pptx
Introduction to ReactJS UI Web Dev .pptxIntroduction to ReactJS UI Web Dev .pptx
Introduction to ReactJS UI Web Dev .pptx
SHAIKIRFAN715544
 
How to create components in ReactJS_.pdf
How to create components in ReactJS_.pdfHow to create components in ReactJS_.pdf
How to create components in ReactJS_.pdf
BOSC Tech Labs
 
React basics
React basicsReact basics
React basics
lakshaya maheshwari
 
React Component
React ComponentReact Component
React Component
RajasreePothula3
 
React state management and side-effects – A Review of Hooks
React state management and side-effects – A Review of HooksReact state management and side-effects – A Review of Hooks
React state management and side-effects – A Review of Hooks
IRJET Journal
 

Similar to React Class Components vs Functional Components: Which is Better? (20)

React
ReactReact
React
 
React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...
 
reatppt.pptx
reatppt.pptxreatppt.pptx
reatppt.pptx
 
React JS Interview Question & Answer
React JS Interview Question & AnswerReact JS Interview Question & Answer
React JS Interview Question & Answer
 
Comparison of Function Components and Class Components in React.js
Comparison of Function Components and Class Components in React.jsComparison of Function Components and Class Components in React.js
Comparison of Function Components and Class Components in React.js
 
React-JS.pptx
React-JS.pptxReact-JS.pptx
React-JS.pptx
 
Copy of React_JS_Notes.pdf
Copy of React_JS_Notes.pdfCopy of React_JS_Notes.pdf
Copy of React_JS_Notes.pdf
 
React Components.docx
React Components.docxReact Components.docx
React Components.docx
 
REACTJS.pdf
REACTJS.pdfREACTJS.pdf
REACTJS.pdf
 
Fundamental concepts of react js
Fundamental concepts of react jsFundamental concepts of react js
Fundamental concepts of react js
 
Introduction to React JS.pptx
Introduction to React JS.pptxIntroduction to React JS.pptx
Introduction to React JS.pptx
 
What are the components in React?
What are the components in React?What are the components in React?
What are the components in React?
 
Choosing Between Functional and Class Components in React: What You Need to Know
Choosing Between Functional and Class Components in React: What You Need to KnowChoosing Between Functional and Class Components in React: What You Need to Know
Choosing Between Functional and Class Components in React: What You Need to Know
 
Importance of Hook in Recat Js.pdf
Importance of Hook in Recat Js.pdfImportance of Hook in Recat Js.pdf
Importance of Hook in Recat Js.pdf
 
Introduction to ReactJS UI Web Dev .pptx
Introduction to ReactJS UI Web Dev .pptxIntroduction to ReactJS UI Web Dev .pptx
Introduction to ReactJS UI Web Dev .pptx
 
How to create components in ReactJS_.pdf
How to create components in ReactJS_.pdfHow to create components in ReactJS_.pdf
How to create components in ReactJS_.pdf
 
ReactJS (1)
ReactJS (1)ReactJS (1)
ReactJS (1)
 
React basics
React basicsReact basics
React basics
 
React Component
React ComponentReact Component
React Component
 
React state management and side-effects – A Review of Hooks
React state management and side-effects – A Review of HooksReact state management and side-effects – A Review of Hooks
React state management and side-effects – A Review of Hooks
 

More from Fibonalabs

Data Sharing Between Child and Parent Components in AngularJS
Data Sharing Between Child and Parent Components in AngularJSData Sharing Between Child and Parent Components in AngularJS
Data Sharing Between Child and Parent Components in AngularJS
Fibonalabs
 
A Complete Guide to Building a Ground-Breaking UX Design Strategy
A Complete Guide to Building a Ground-Breaking UX Design StrategyA Complete Guide to Building a Ground-Breaking UX Design Strategy
A Complete Guide to Building a Ground-Breaking UX Design Strategy
Fibonalabs
 
Measures to ensure Cyber Security in a serverless environment
Measures to ensure Cyber Security in a serverless environmentMeasures to ensure Cyber Security in a serverless environment
Measures to ensure Cyber Security in a serverless environment
Fibonalabs
 
Simplifying CRUD operations using budibase
Simplifying CRUD operations using budibaseSimplifying CRUD operations using budibase
Simplifying CRUD operations using budibase
Fibonalabs
 
How to implement Micro-frontends using Qiankun
How to implement Micro-frontends using QiankunHow to implement Micro-frontends using Qiankun
How to implement Micro-frontends using Qiankun
Fibonalabs
 
Different Cloud Computing Services Used At Fibonalabs
Different Cloud Computing Services Used At FibonalabsDifferent Cloud Computing Services Used At Fibonalabs
Different Cloud Computing Services Used At Fibonalabs
Fibonalabs
 
How Can A Startup Benefit From Collaborating With A UX Design Partner
How Can A Startup Benefit From Collaborating With A UX Design PartnerHow Can A Startup Benefit From Collaborating With A UX Design Partner
How Can A Startup Benefit From Collaborating With A UX Design Partner
Fibonalabs
 
How to make React Applications SEO-friendly
How to make React Applications SEO-friendlyHow to make React Applications SEO-friendly
How to make React Applications SEO-friendly
Fibonalabs
 
10 Heuristic Principles
10 Heuristic Principles10 Heuristic Principles
10 Heuristic Principles
Fibonalabs
 
Push Notifications: How to add them to a Flutter App
Push Notifications: How to add them to a Flutter AppPush Notifications: How to add them to a Flutter App
Push Notifications: How to add them to a Flutter App
Fibonalabs
 
Key Skills Required for Data Engineering
Key Skills Required for Data EngineeringKey Skills Required for Data Engineering
Key Skills Required for Data Engineering
Fibonalabs
 
Ways for UX Design Iterations: Innovate Faster & Better
Ways for UX Design Iterations: Innovate Faster & BetterWays for UX Design Iterations: Innovate Faster & Better
Ways for UX Design Iterations: Innovate Faster & Better
Fibonalabs
 
Factors that could impact conversion rate in UX Design
Factors that could impact conversion rate in UX DesignFactors that could impact conversion rate in UX Design
Factors that could impact conversion rate in UX Design
Fibonalabs
 
Information Architecture in UX: To offer Delightful and Meaningful User Exper...
Information Architecture in UX: To offer Delightful and Meaningful User Exper...Information Architecture in UX: To offer Delightful and Meaningful User Exper...
Information Architecture in UX: To offer Delightful and Meaningful User Exper...
Fibonalabs
 
Cloud Computing Architecture: Components, Importance, and Tips
Cloud Computing Architecture: Components, Importance, and TipsCloud Computing Architecture: Components, Importance, and Tips
Cloud Computing Architecture: Components, Importance, and Tips
Fibonalabs
 
Choose the Best Agile Product Development Method for a Successful Business
Choose the Best Agile Product Development Method for a Successful BusinessChoose the Best Agile Product Development Method for a Successful Business
Choose the Best Agile Product Development Method for a Successful Business
Fibonalabs
 
Atomic Design: Effective Way of Designing UI
Atomic Design: Effective Way of Designing UIAtomic Design: Effective Way of Designing UI
Atomic Design: Effective Way of Designing UI
Fibonalabs
 
Agile Software Development with Scrum_ A Complete Guide to The Steps in Agile...
Agile Software Development with Scrum_ A Complete Guide to The Steps in Agile...Agile Software Development with Scrum_ A Complete Guide to The Steps in Agile...
Agile Software Development with Scrum_ A Complete Guide to The Steps in Agile...
Fibonalabs
 
7 Psychology Theories in UX to Provide Better User Experience
7 Psychology Theories in UX to Provide Better User Experience7 Psychology Theories in UX to Provide Better User Experience
7 Psychology Theories in UX to Provide Better User Experience
Fibonalabs
 
Moving From JavaScript to TypeScript: Things Developers Should Know
Moving From JavaScript to TypeScript: Things Developers Should KnowMoving From JavaScript to TypeScript: Things Developers Should Know
Moving From JavaScript to TypeScript: Things Developers Should Know
Fibonalabs
 

More from Fibonalabs (20)

Data Sharing Between Child and Parent Components in AngularJS
Data Sharing Between Child and Parent Components in AngularJSData Sharing Between Child and Parent Components in AngularJS
Data Sharing Between Child and Parent Components in AngularJS
 
A Complete Guide to Building a Ground-Breaking UX Design Strategy
A Complete Guide to Building a Ground-Breaking UX Design StrategyA Complete Guide to Building a Ground-Breaking UX Design Strategy
A Complete Guide to Building a Ground-Breaking UX Design Strategy
 
Measures to ensure Cyber Security in a serverless environment
Measures to ensure Cyber Security in a serverless environmentMeasures to ensure Cyber Security in a serverless environment
Measures to ensure Cyber Security in a serverless environment
 
Simplifying CRUD operations using budibase
Simplifying CRUD operations using budibaseSimplifying CRUD operations using budibase
Simplifying CRUD operations using budibase
 
How to implement Micro-frontends using Qiankun
How to implement Micro-frontends using QiankunHow to implement Micro-frontends using Qiankun
How to implement Micro-frontends using Qiankun
 
Different Cloud Computing Services Used At Fibonalabs
Different Cloud Computing Services Used At FibonalabsDifferent Cloud Computing Services Used At Fibonalabs
Different Cloud Computing Services Used At Fibonalabs
 
How Can A Startup Benefit From Collaborating With A UX Design Partner
How Can A Startup Benefit From Collaborating With A UX Design PartnerHow Can A Startup Benefit From Collaborating With A UX Design Partner
How Can A Startup Benefit From Collaborating With A UX Design Partner
 
How to make React Applications SEO-friendly
How to make React Applications SEO-friendlyHow to make React Applications SEO-friendly
How to make React Applications SEO-friendly
 
10 Heuristic Principles
10 Heuristic Principles10 Heuristic Principles
10 Heuristic Principles
 
Push Notifications: How to add them to a Flutter App
Push Notifications: How to add them to a Flutter AppPush Notifications: How to add them to a Flutter App
Push Notifications: How to add them to a Flutter App
 
Key Skills Required for Data Engineering
Key Skills Required for Data EngineeringKey Skills Required for Data Engineering
Key Skills Required for Data Engineering
 
Ways for UX Design Iterations: Innovate Faster & Better
Ways for UX Design Iterations: Innovate Faster & BetterWays for UX Design Iterations: Innovate Faster & Better
Ways for UX Design Iterations: Innovate Faster & Better
 
Factors that could impact conversion rate in UX Design
Factors that could impact conversion rate in UX DesignFactors that could impact conversion rate in UX Design
Factors that could impact conversion rate in UX Design
 
Information Architecture in UX: To offer Delightful and Meaningful User Exper...
Information Architecture in UX: To offer Delightful and Meaningful User Exper...Information Architecture in UX: To offer Delightful and Meaningful User Exper...
Information Architecture in UX: To offer Delightful and Meaningful User Exper...
 
Cloud Computing Architecture: Components, Importance, and Tips
Cloud Computing Architecture: Components, Importance, and TipsCloud Computing Architecture: Components, Importance, and Tips
Cloud Computing Architecture: Components, Importance, and Tips
 
Choose the Best Agile Product Development Method for a Successful Business
Choose the Best Agile Product Development Method for a Successful BusinessChoose the Best Agile Product Development Method for a Successful Business
Choose the Best Agile Product Development Method for a Successful Business
 
Atomic Design: Effective Way of Designing UI
Atomic Design: Effective Way of Designing UIAtomic Design: Effective Way of Designing UI
Atomic Design: Effective Way of Designing UI
 
Agile Software Development with Scrum_ A Complete Guide to The Steps in Agile...
Agile Software Development with Scrum_ A Complete Guide to The Steps in Agile...Agile Software Development with Scrum_ A Complete Guide to The Steps in Agile...
Agile Software Development with Scrum_ A Complete Guide to The Steps in Agile...
 
7 Psychology Theories in UX to Provide Better User Experience
7 Psychology Theories in UX to Provide Better User Experience7 Psychology Theories in UX to Provide Better User Experience
7 Psychology Theories in UX to Provide Better User Experience
 
Moving From JavaScript to TypeScript: Things Developers Should Know
Moving From JavaScript to TypeScript: Things Developers Should KnowMoving From JavaScript to TypeScript: Things Developers Should Know
Moving From JavaScript to TypeScript: Things Developers Should Know
 

Recently uploaded

When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
Abida Shariff
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
Bhaskar Mitra
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi
Fwdays
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
Product School
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
DianaGray10
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
Frank van Harmelen
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Tobias Schneck
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
Ralf Eggert
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 

Recently uploaded (20)

When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 

React Class Components vs Functional Components: Which is Better?

  • 1. React Class Components vs functional Components: Which is Better? https://fibonalabs.com/
  • 2.
  • 3. To understand class and functional components, we should first understand how these to React components evolved. React is an open-source front-end JS library for building interactive user interfaces. Some of the famous features are the virtual DOM, one-way data binding, JSX, reusable components, stable code, fast rendering of UI elements, and performance optimization. Along with these advantages, React also offers two types of components that the developers could use to create UI elements. It can be assumed that both provide the same opportunities for writing UI elements. But it isn't true.
  • 4. The real reason was that class components were the only option to develop complex code. The reason was with class components that you get additional features like state, while functional components didn’t have such an option. However, the situation changed with React v16.8 which was released in 2019. It took the development of the functional components to a new level. React offered hooks for functional components. The introduction of hooks made it possible to write complex code using only functions.
  • 5. Functional Components Props are inputs for both types of components. One of the main tasks of props is to pass information from one component to another. However, props are read-only. That means all React components shouldn’t change their props value and must return the same result. Components that respect their props are called “pure”. That rule works both for class and function components. Another way of declaring functional components is by using an arrow function. Arrow functions have some advantages: 1. It looks compact. Functions are easier to read and write.
  • 6.
  • 7. Class Components Props work well with both class and functional components. Additional benefits of class components are that they have a state and lifecycle. This is the reason they are called “stateful” components. The state of a component is an object that holds some information and controls the behaviour of the component. The difference between props and state is that props don’t change over time, but the state holds the data that can be changed over time and changes the component rendering.
  • 8.
  • 9. Difference between Functional and Class Components ● State and Lifecycle: The difference between functional and class components was that class components provide features such as setState() and lifecycle methods componentDidMount(), componentWillUnmoun(), etc., while functional components don’t. Functional components are plain JavaScript functions that accept props and return React elements, while class components are JavaScript classes that extend React. A component that has a render method. Both state and lifecycle methods come from React. Component, hence available in only
  • 10. However, things changed with the introduction of Hooks. To replace setState (in-class components) React offers useState Hook for functional components. To work with components lifecycle, classes have methods like componentDidMount, componentWillUnmount, componentWillUpdate, and componentDidUpdate whereas functional components have a hook called useEffect. ● Syntax: ➢ Declaration: Functional components are JavaScript functions whereas Class components are classes that extend React.Component.
  • 11.
  • 12. ➢ The Way Props are Passes: Working with functional components, we pass the props as an argument of the function using “props.name”. With class components, we need to add this. to refer to props.
  • 14. ➢ Handling State: To handle state functional components in React have a useState()Hook. We assign the initial count equal to 0 and set the method setCount() that increments by one every time we click a button. The type of argument can be a number, string, object, or null. Class components use the setState() function which requires a constructor and this keyword to refer to the state.
  • 17. ➢ Hoisting Works Only for Functional Components: Hoisting is a concept where variables and function declarations are moved to the top that allows you to access the variable or function first and then declare it. Javascript doesn’t modify the code, instead, it puts declarations in memory during the compilation that allows calling a function before you declare it. Whereas in classes, if we try to get access to a class before the declaration it throws a ReferenceError exception.
  • 18.
  • 19. ➢ Running Tests: There are two most popular utilities for testing - Enzyme and Jest. The enzyme is a JS testing utility for React that allows testing React renders. Jest is a JS testing framework for creating, running, and structuring tests. These two utilities work well with both types of components. There are some specific things to keep in mind in running tests for functional components, like, the state hooks are internal to the component and can’t be tested by calling them. Same with methods and functions.
  • 20. ➢ Performance Difference: People think that functional components have better performance than class components. React functional components are simple Objects with 2 properties: type (string) and props (object). To render such a component React needs to call the function and pass down the props. Class components are instances of React.Component with the constructor in it and complex flows for manipulating state and lifecycle.
  • 21. Advantages of a Functional Component Over a Class Component 1. A functional component is easier to read and test as they are just plain JS functions. 2. You write less code. 3. The React team stated that there may be a performance boost for a functional component in future releases.
  • 22. In a Nutshell ● Earlier, class components were the only option to add states to components and manipulate the lifecycle. However, since the introduction of React Hooks, now we can add the same functionality to function components. ● The major difference is the syntax. It is related to the way we declare components, pass props, handle states, and manage the lifecycle. ● Functional components capture the props and state by default.
  • 23. ● Functional components require less code to write. It's subjective and depends on the developer on how he wants to write the code. ● There are two popular tools to test functional and class components: Enzyme and Jest. ● There is not much difference in render time between class and functional components.