SlideShare a Scribd company logo
React – Let’s “Hook” up
With the introduction of React 16.8 in 2018, React team came up with a new concept of
“Hooks”. In this blog we are going to tell the reason behind creating hooks and also how to
use them in a React application.
In React, we can create two types of components namely, Functional or Stateless component and
the other one is Class or Stateful component.
Mostly, we create a class component when we have a requirement to maintain a state or to use life
cycle methods in our component. But in reality, there is no actual class present in React which
means classes in React are just syntactic sugar to provide resemblance with more popular OOPs
classes and these class components will eventually convert to functional components.
But in order to achieve resemblance with OOPs classes, the classes in React has created more
damage than the advantages it has to offer.
Reasons to avoid class components and creation of Hooks: -
1. Huge components hence difficult to manage and debug.
2. Lots of Boiler Plate is required to create a single class component.
3. “Binding” and “this” in class components are confusing.
4. Unused methods in the class component are not removed in the minified files
5. Class components makes hot reloading difficult.
6. Class components substantially reduces compiler optimization capability.
To overcome these impediments, React team came up with a new concept called “Hooks”.
According to React Documents,
A Hook is a special function that lets you “hook into” React features.
The idea behind Hooks is to provide all the capabilities that we get in a class component to a
functional component. The two most common capabilities of class components are state and life
cycle methods. Below I have given some examples which shows how we can use hooks to achieve
the two aforementioned class component capabilities.
State with Hooks
Example 1
Let’s take an example to understand how to create and use state in a functional component with the
help of hooks : -
In this example, we can observe a new term called ‘useState’ that we have de-structured from the
react API. This useState is a hook which helps in creating a state in a stateless or functional
component.
Basically useState is a method in which we can pass argument and that argument will be treated as
the default or initial value of the state variable. If we don’t pass any argument than undefined will
be assigned to the state variable. Here that state variable is ‘increment’. useState method returns a
pair of values: the current state and the method to update it.
const [ increment, setIncrement ] = useState(0)
This line of code will create a state variable called increment and allocate 0 value to that variable.
To assign a different value to this variable we have defined a method called ‘setIncrement’. So
whenever we want to change the value of this variable we can do it only through setIncrement as we
did while clicking the button.
Example 2
Let’s take another example in which we will use useState more than once to create multiple state
variables :-
Here in this example we are using two useState to create two separate state variables and also we
have two different methods to assign new values to these state variables. In the similar fashion we
can create any number of state variables using multiple useState hooks and assign them different
default values and also create their separate methods to handle each one of them.
Life cycle methods with Hooks
To provide life cycle methods capability to functional component React provide us a hook called
useEffect. useEffect hook serves the same purpose as componentDidMount, componentDidUpdate
and componentWillUnmount. Let’s take some example to understand the usage of useEffect hook :
-
Example 1
In this example, we can observe a new term useEffect that we have de-structured from the React
API. This useEffect is a hook which helps in providing componentDidMount as well as
componentDidUpdate life cycle method capability to this functional component.
Basically useEffect is a method which takes a function as an argument. In this example after the
component is rendered useEffect will be called for the first time that means it is acting as the
componentDidMount life cycle method and it will change the title of the document. Now, if the user
clicks the button, state variable ‘increment’ value be updated and the component will be rendered
again. After rendering, useEffect will be called again but this time it will act as
componentDidUpdate. So now every time the component is rendered, useEffect will be called.
Example 2
Similar to useState, we can use multiple useEffect method in a single functional component. Let’s
take another example to explain that scenario : -
Here in this example, we are using more than one useEffect method and both will run after each
render.
Example 3
The useEffect method can take another argument of type array. This argument is used as a reference
to compare current value of the variable specified in the array with the previous value of the same
variable. This scenario is used to conditionally trigger the first argument of the useEffect method.
In this example we have specified the state variable ‘increment’ as the second argument of the
useEffect hook, so now the useEffect first argument will only trigger if the value of the ‘increment’
will change. When we click on the second button, state variable ‘isValid’ will change and hence
component will render again but useEffect will check whether state variable ‘increment’ has
changed or not and don’t find any change in that so it will not execute its first argument which is a
method.
There can also be multiple arguments present in the array, if any one of the array variables value
changes then useEffect will be triggered.
Conclusion
So from the previous examples we can conclude we are able to overcome all the impediments faced
due to class components and hence in future hooks will be used in more and more React projects.
There are also many advanced hooks are present in the Hooks API such as useReducer,
useCallback, useMemo etc. You can learn about in depth from the React official site which is given
below :-
https://reactjs.org/docs/hooks-reference.htm

More Related Content

Similar to React – Let’s “Hook” up (20)

PDF
React Hooks: Enhancing Functional Components with useState, useEffect, and Cu...
Akshat Pegwar
 
PPTX
React-JS.pptx
AnmolPandita7
 
PDF
React.js hooks feb 23rd meetup
Jaikant Kumaran
 
PPTX
Green Custard Friday Talk 21: React Hooks
Green Custard
 
PDF
Basics of React Hooks.pptx.pdf
Knoldus Inc.
 
PPTX
React Hooks
Erez Cohen
 
PPTX
React hooks
Ramy ElBasyouni
 
PPTX
ReactJs Training in Hyderabad | ReactJS Training
eshwarvisualpath
 
PDF
React new features and intro to Hooks
Soluto
 
PPTX
React hooks
Assaf Gannon
 
PDF
Hello, React Hooks!
Heejong Ahn
 
PDF
Important React Hooks
Knoldus Inc.
 
PDF
react-hooks.pdf
chengbo xu
 
PDF
Importance of Hook in Recat Js.pdf
Nishaadequateinfosof
 
PPTX
class based component.pptx
saikatsamanta49
 
PDF
React hooks Episode #1: An introduction.
ManojSatishKumar
 
PPTX
Understanding react hooks
Maulik Shah
 
PDF
Full Stack React Workshop [CSSC x GDSC]
GDSC UofT Mississauga
 
PPTX
TRAINING pptt efwoiefo weoifjoiewjfoifjow.pptx
PrathamSharma77833
 
PDF
How do we use hooks
Jim Liu
 
React Hooks: Enhancing Functional Components with useState, useEffect, and Cu...
Akshat Pegwar
 
React-JS.pptx
AnmolPandita7
 
React.js hooks feb 23rd meetup
Jaikant Kumaran
 
Green Custard Friday Talk 21: React Hooks
Green Custard
 
Basics of React Hooks.pptx.pdf
Knoldus Inc.
 
React Hooks
Erez Cohen
 
React hooks
Ramy ElBasyouni
 
ReactJs Training in Hyderabad | ReactJS Training
eshwarvisualpath
 
React new features and intro to Hooks
Soluto
 
React hooks
Assaf Gannon
 
Hello, React Hooks!
Heejong Ahn
 
Important React Hooks
Knoldus Inc.
 
react-hooks.pdf
chengbo xu
 
Importance of Hook in Recat Js.pdf
Nishaadequateinfosof
 
class based component.pptx
saikatsamanta49
 
React hooks Episode #1: An introduction.
ManojSatishKumar
 
Understanding react hooks
Maulik Shah
 
Full Stack React Workshop [CSSC x GDSC]
GDSC UofT Mississauga
 
TRAINING pptt efwoiefo weoifjoiewjfoifjow.pptx
PrathamSharma77833
 
How do we use hooks
Jim Liu
 

More from InnovationM (20)

PDF
How to use data binding in android
InnovationM
 
PDF
Capture image on eye blink
InnovationM
 
PDF
Mob x in react
InnovationM
 
PDF
How to use geolocation in react native apps
InnovationM
 
PDF
Android 8 behavior changes
InnovationM
 
PDF
Understanding of react fiber architecture
InnovationM
 
PDF
Automatic reference counting (arc) and memory management in swift
InnovationM
 
PDF
Firebase crashlytics integration in iOS swift (dSYM File Required Problem Res...
InnovationM
 
PDF
How prototype works in java script?
InnovationM
 
PDF
Razorpay Payment Gateway Integration In iOS Swift
InnovationM
 
PDF
Paytm integration in swift
InnovationM
 
PDF
Line Messaging API Integration with Spring-Boot
InnovationM
 
PDF
Basic fundamental of ReactJS
InnovationM
 
PDF
Basic Fundamental of Redux
InnovationM
 
PDF
Integration of Highcharts with React ( JavaScript library )
InnovationM
 
PDF
Serialization & De-serialization in Java
InnovationM
 
PDF
Concept of Stream API Java 1.8
InnovationM
 
PDF
How to Make Each Round of Testing Count?
InnovationM
 
PDF
Model View Presenter For Android
InnovationM
 
PDF
Retrofit Library In Android
InnovationM
 
How to use data binding in android
InnovationM
 
Capture image on eye blink
InnovationM
 
Mob x in react
InnovationM
 
How to use geolocation in react native apps
InnovationM
 
Android 8 behavior changes
InnovationM
 
Understanding of react fiber architecture
InnovationM
 
Automatic reference counting (arc) and memory management in swift
InnovationM
 
Firebase crashlytics integration in iOS swift (dSYM File Required Problem Res...
InnovationM
 
How prototype works in java script?
InnovationM
 
Razorpay Payment Gateway Integration In iOS Swift
InnovationM
 
Paytm integration in swift
InnovationM
 
Line Messaging API Integration with Spring-Boot
InnovationM
 
Basic fundamental of ReactJS
InnovationM
 
Basic Fundamental of Redux
InnovationM
 
Integration of Highcharts with React ( JavaScript library )
InnovationM
 
Serialization & De-serialization in Java
InnovationM
 
Concept of Stream API Java 1.8
InnovationM
 
How to Make Each Round of Testing Count?
InnovationM
 
Model View Presenter For Android
InnovationM
 
Retrofit Library In Android
InnovationM
 
Ad

Recently uploaded (20)

PPTX
UI5Con 2025 - Get to Know Your UI5 Tooling
Wouter Lemaire
 
PDF
Bitcoin+ Escalando sin concesiones - Parte 1
Fernando Paredes García
 
PPTX
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
PDF
Building Resilience with Digital Twins : Lessons from Korea
SANGHEE SHIN
 
PPTX
Lecture 5 - Agentic AI and model context protocol.pptx
Dr. LAM Yat-fai (林日辉)
 
PDF
OpenInfra ID 2025 - Are Containers Dying? Rethinking Isolation with MicroVMs.pdf
Muhammad Yuga Nugraha
 
PDF
2025-07-15 EMEA Volledig Inzicht Dutch Webinar
ThousandEyes
 
PPTX
UI5Con 2025 - Beyond UI5 Controls with the Rise of Web Components
Wouter Lemaire
 
PPTX
The Yotta x CloudStack Advantage: Scalable, India-First Cloud
ShapeBlue
 
PDF
Shuen Mei Parth Sharma Boost Productivity, Innovation and Efficiency wit...
AWS Chicago
 
PPTX
Top Managed Service Providers in Los Angeles
Captain IT
 
PDF
TrustArc Webinar - Data Privacy Trends 2025: Mid-Year Insights & Program Stra...
TrustArc
 
PPTX
Darren Mills The Migration Modernization Balancing Act: Navigating Risks and...
AWS Chicago
 
PDF
How a Code Plagiarism Checker Protects Originality in Programming
Code Quiry
 
PDF
The Past, Present & Future of Kenya's Digital Transformation
Moses Kemibaro
 
PDF
UiPath vs Other Automation Tools Meeting Presentation.pdf
Tracy Dixon
 
PPTX
Extensions Framework (XaaS) - Enabling Orchestrate Anything
ShapeBlue
 
PDF
Novus-Safe Pro: Brochure-What is Novus Safe Pro?.pdf
Novus Hi-Tech
 
PPTX
Earn Agentblazer Status with Slack Community Patna.pptx
SanjeetMishra29
 
PDF
GITLAB-CICD_For_Professionals_KodeKloud.pdf
deepaktyagi0048
 
UI5Con 2025 - Get to Know Your UI5 Tooling
Wouter Lemaire
 
Bitcoin+ Escalando sin concesiones - Parte 1
Fernando Paredes García
 
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
Building Resilience with Digital Twins : Lessons from Korea
SANGHEE SHIN
 
Lecture 5 - Agentic AI and model context protocol.pptx
Dr. LAM Yat-fai (林日辉)
 
OpenInfra ID 2025 - Are Containers Dying? Rethinking Isolation with MicroVMs.pdf
Muhammad Yuga Nugraha
 
2025-07-15 EMEA Volledig Inzicht Dutch Webinar
ThousandEyes
 
UI5Con 2025 - Beyond UI5 Controls with the Rise of Web Components
Wouter Lemaire
 
The Yotta x CloudStack Advantage: Scalable, India-First Cloud
ShapeBlue
 
Shuen Mei Parth Sharma Boost Productivity, Innovation and Efficiency wit...
AWS Chicago
 
Top Managed Service Providers in Los Angeles
Captain IT
 
TrustArc Webinar - Data Privacy Trends 2025: Mid-Year Insights & Program Stra...
TrustArc
 
Darren Mills The Migration Modernization Balancing Act: Navigating Risks and...
AWS Chicago
 
How a Code Plagiarism Checker Protects Originality in Programming
Code Quiry
 
The Past, Present & Future of Kenya's Digital Transformation
Moses Kemibaro
 
UiPath vs Other Automation Tools Meeting Presentation.pdf
Tracy Dixon
 
Extensions Framework (XaaS) - Enabling Orchestrate Anything
ShapeBlue
 
Novus-Safe Pro: Brochure-What is Novus Safe Pro?.pdf
Novus Hi-Tech
 
Earn Agentblazer Status with Slack Community Patna.pptx
SanjeetMishra29
 
GITLAB-CICD_For_Professionals_KodeKloud.pdf
deepaktyagi0048
 
Ad

React – Let’s “Hook” up

  • 1. React – Let’s “Hook” up With the introduction of React 16.8 in 2018, React team came up with a new concept of “Hooks”. In this blog we are going to tell the reason behind creating hooks and also how to use them in a React application. In React, we can create two types of components namely, Functional or Stateless component and the other one is Class or Stateful component. Mostly, we create a class component when we have a requirement to maintain a state or to use life cycle methods in our component. But in reality, there is no actual class present in React which means classes in React are just syntactic sugar to provide resemblance with more popular OOPs classes and these class components will eventually convert to functional components. But in order to achieve resemblance with OOPs classes, the classes in React has created more damage than the advantages it has to offer. Reasons to avoid class components and creation of Hooks: - 1. Huge components hence difficult to manage and debug. 2. Lots of Boiler Plate is required to create a single class component. 3. “Binding” and “this” in class components are confusing. 4. Unused methods in the class component are not removed in the minified files 5. Class components makes hot reloading difficult. 6. Class components substantially reduces compiler optimization capability. To overcome these impediments, React team came up with a new concept called “Hooks”. According to React Documents, A Hook is a special function that lets you “hook into” React features. The idea behind Hooks is to provide all the capabilities that we get in a class component to a functional component. The two most common capabilities of class components are state and life cycle methods. Below I have given some examples which shows how we can use hooks to achieve the two aforementioned class component capabilities. State with Hooks Example 1 Let’s take an example to understand how to create and use state in a functional component with the help of hooks : - In this example, we can observe a new term called ‘useState’ that we have de-structured from the react API. This useState is a hook which helps in creating a state in a stateless or functional
  • 2. component. Basically useState is a method in which we can pass argument and that argument will be treated as the default or initial value of the state variable. If we don’t pass any argument than undefined will be assigned to the state variable. Here that state variable is ‘increment’. useState method returns a pair of values: the current state and the method to update it. const [ increment, setIncrement ] = useState(0) This line of code will create a state variable called increment and allocate 0 value to that variable. To assign a different value to this variable we have defined a method called ‘setIncrement’. So whenever we want to change the value of this variable we can do it only through setIncrement as we did while clicking the button. Example 2 Let’s take another example in which we will use useState more than once to create multiple state variables :- Here in this example we are using two useState to create two separate state variables and also we have two different methods to assign new values to these state variables. In the similar fashion we can create any number of state variables using multiple useState hooks and assign them different default values and also create their separate methods to handle each one of them. Life cycle methods with Hooks To provide life cycle methods capability to functional component React provide us a hook called useEffect. useEffect hook serves the same purpose as componentDidMount, componentDidUpdate and componentWillUnmount. Let’s take some example to understand the usage of useEffect hook : - Example 1 In this example, we can observe a new term useEffect that we have de-structured from the React API. This useEffect is a hook which helps in providing componentDidMount as well as componentDidUpdate life cycle method capability to this functional component.
  • 3. Basically useEffect is a method which takes a function as an argument. In this example after the component is rendered useEffect will be called for the first time that means it is acting as the componentDidMount life cycle method and it will change the title of the document. Now, if the user clicks the button, state variable ‘increment’ value be updated and the component will be rendered again. After rendering, useEffect will be called again but this time it will act as componentDidUpdate. So now every time the component is rendered, useEffect will be called. Example 2 Similar to useState, we can use multiple useEffect method in a single functional component. Let’s take another example to explain that scenario : - Here in this example, we are using more than one useEffect method and both will run after each render. Example 3 The useEffect method can take another argument of type array. This argument is used as a reference to compare current value of the variable specified in the array with the previous value of the same
  • 4. variable. This scenario is used to conditionally trigger the first argument of the useEffect method. In this example we have specified the state variable ‘increment’ as the second argument of the useEffect hook, so now the useEffect first argument will only trigger if the value of the ‘increment’ will change. When we click on the second button, state variable ‘isValid’ will change and hence component will render again but useEffect will check whether state variable ‘increment’ has changed or not and don’t find any change in that so it will not execute its first argument which is a method. There can also be multiple arguments present in the array, if any one of the array variables value changes then useEffect will be triggered. Conclusion So from the previous examples we can conclude we are able to overcome all the impediments faced due to class components and hence in future hooks will be used in more and more React projects. There are also many advanced hooks are present in the Hooks API such as useReducer, useCallback, useMemo etc. You can learn about in depth from the React official site which is given below :- https://reactjs.org/docs/hooks-reference.htm