SlideShare a Scribd company logo
React Class Components vs
functional Components: Which
is Better?
https://fibonalabs.com/
React Class Components vs Functional Components: Which is Better?
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.
React Class Components vs Functional Components: Which is Better?
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.
React Class Components vs Functional Components: Which is Better?
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.
React Class Components vs Functional Components: Which is Better?
➢ 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.
React Class Components vs Functional Components: Which is Better?
➢ 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

PPTX
React hooks
PPTX
React js programming concept
PPTX
React JS - A quick introduction tutorial
PPTX
React workshop
PDF
Asynchronous JavaScript Programming
PPTX
React js for beginners
PDF
React js
PPTX
React state
React hooks
React js programming concept
React JS - A quick introduction tutorial
React workshop
Asynchronous JavaScript Programming
React js for beginners
React js
React state

What's hot (20)

PPTX
Introduction to React JS for beginners
PPTX
Introduction to React
PPTX
React JS: A Secret Preview
PPTX
Its time to React.js
PPTX
Introduction to React JS
PPTX
JS Event Loop
PDF
ReactJS presentation
PPTX
Understanding react hooks
PDF
React new features and intro to Hooks
PDF
Important React Hooks
PPTX
Intro to React
PPTX
Intro to React
PPTX
React hooks
PPTX
What is component in reactjs
PDF
Understanding react hooks
PPTX
Reactjs
PPTX
Introduction to react_js
PPTX
ReactJS presentation.pptx
PDF
Introduction to React JS
Introduction to React JS for beginners
Introduction to React
React JS: A Secret Preview
Its time to React.js
Introduction to React JS
JS Event Loop
ReactJS presentation
Understanding react hooks
React new features and intro to Hooks
Important React Hooks
Intro to React
Intro to React
React hooks
What is component in reactjs
Understanding react hooks
Reactjs
Introduction to react_js
ReactJS presentation.pptx
Introduction to React JS
Ad

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

PPTX
PPTX
React JS; all concepts. Contains React Features, JSX, functional & Class comp...
PDF
100 React Interview questions 2024.pptx.pdf
PPTX
React JS Interview Question & Answer
PDF
unit 3_Adv WTAdvanced Web Tecg Design_HTML_CSS_JAVASCRIPT_AJAX_PPT.pdf
DOCX
Comparison of Function Components and Class Components in React.js
PPTX
React-JS.pptx
PDF
React Interview Question PDF By ScholarHat
PDF
Copy of React_JS_Notes.pdf
DOCX
React Components.docx
PDF
REACTJS.pdf
PDF
Fundamental concepts of react js
PPTX
Introduction to React JS.pptx
PPTX
What are the components in React?
PDF
Choosing Between Functional and Class Components in React: What You Need to Know
PDF
Importance of Hook in Recat Js.pdf
PPTX
React js
PDF
Understanding React hooks | Walkingtree Technologies
PPTX
Introduction to ReactJS UI Web Dev .pptx
React JS; all concepts. Contains React Features, JSX, functional & Class comp...
100 React Interview questions 2024.pptx.pdf
React JS Interview Question & Answer
unit 3_Adv WTAdvanced Web Tecg Design_HTML_CSS_JAVASCRIPT_AJAX_PPT.pdf
Comparison of Function Components and Class Components in React.js
React-JS.pptx
React Interview Question PDF By ScholarHat
Copy of React_JS_Notes.pdf
React Components.docx
REACTJS.pdf
Fundamental concepts of react js
Introduction to React JS.pptx
What are the components in React?
Choosing Between Functional and Class Components in React: What You Need to Know
Importance of Hook in Recat Js.pdf
React js
Understanding React hooks | Walkingtree Technologies
Introduction to ReactJS UI Web Dev .pptx
Ad

More from Fibonalabs (20)

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

Recently uploaded (20)

PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Chapter 3 Spatial Domain Image Processing.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Modernizing your data center with Dell and AMD
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Big Data Technologies - Introduction.pptx
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Empathic Computing: Creating Shared Understanding
PPTX
A Presentation on Artificial Intelligence
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
Chapter 3 Spatial Domain Image Processing.pdf
The AUB Centre for AI in Media Proposal.docx
Modernizing your data center with Dell and AMD
Network Security Unit 5.pdf for BCA BBA.
Big Data Technologies - Introduction.pptx
MYSQL Presentation for SQL database connectivity
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Diabetes mellitus diagnosis method based random forest with bat algorithm
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Per capita expenditure prediction using model stacking based on satellite ima...
Empathic Computing: Creating Shared Understanding
A Presentation on Artificial Intelligence
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Dropbox Q2 2025 Financial Results & Investor Presentation
20250228 LYD VKU AI Blended-Learning.pptx
Digital-Transformation-Roadmap-for-Companies.pptx
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
How UI/UX Design Impacts User Retention in Mobile Apps.pdf

React Class Components vs Functional Components: Which is Better?

  • 1. React Class Components vs functional Components: Which is Better? https://fibonalabs.com/
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.