SlideShare a Scribd company logo
2
Most read
3
Most read
19
Most read
React Hooks
Understanding hooks
TOC
Overview
useState and uses
useEffect and uses
useReducer and uses
Custom hooks
Useful hooks
FAQ
Demo: https://jolly-perlman-98367f.netlify.com
Overview
Hooks are a new feature proposal that lets you use
state and other React features without writing a class. They’re currently
in React v16.7.0-alpha and being discussed in an open RFC
Understanding the problems
01 Hooks lets us use state inside functional component, previously if we need state
in any component we need to make class based component.
02 In functional component we were unable to use component’s life cycle hooks
Like componentDidUnmount, componentDidUpdate.
03 With hooks now our functional components are no more stateless component
04
Classes confuse both people and machines, In addition to making code reuse
and code organization more difficult, we’ve found that classes can be a large
barrier to learning React. You have to understand how this works in JavaScript,
which is very different from how it works in most languages. You have to
remember to bind the event handlers.
useState and uses
Here, useState is a Hook. We call it inside a function
component to add some local state to it. React will preserve
this state between re-renders. useState returns a pair: the
current state value and a function that lets you update it. You
can call this function from an event handler or somewhere
else. It’s similar to this.setState in a class, except it doesn’t
merge the old and new state together.
The only argument to useState is the initial state. In the
example above, it is 0 because our counter starts from zero.
Note that unlike this.state, the state here doesn’t have to be an
object — although it can be if you want. The initial state
argument is only used during the first render.
Some key points
● we need to use useState function to have state
● We can use String, Number, Array or Object in useState
● useState return array which we destructure, which is also called array destructuring.
const [state,setState] = useState('samundra') // ['samundra', function(state){ }]
● Doesn't work with class
● Doesn't work outside of component, on any vanilla javascript function description
● uses Linked List data structure
● Placement of useState matters
useEffect and uses
You’ve likely performed data fetching, subscriptions, or
manually changing the DOM from React components before.
We call these operations “side effects” (or “effects” for short)
because they can affect other components and can’t be done
during rendering.
The Effect Hook, useEffect, adds the ability to perform
side effects from a function component. It serves the same
purpose as componentDidMount, componentDidUpdate,
and componentWillUnmount in React classes, but unified
into a single API.
Some key points
● useEffect is effect which is equal to componentDidMount and ComponentDidUpdate.
● which means useEffect will run on mount and update
● will run only after the DOM is applied or DOM mutation is done.
● every time whenever a local state is changed this effect will run.
● we can make our effect run depending on other state by passing it in second argument
as array
● We can make effect run only once by passing empty array in second argument
useReducer and uses
An alternative to useState. Accepts a reducer of type (state,
action) => newState, and returns the current state paired with
a dispatch method. (If you’re familiar with Redux, you already
know how this works.)
Some key points
● useReducer is an another hook provided by react
● when we want to manage state in single object we can use it
● Inspired by redux, we may not need redux if our app is small and we need some state
management
Some key points
● useReducer is an another hook provided by react
● when we want to manage state in single object we can use it
● Inspired by redux, we may not need redux if our app is small and we need some state
management
Custom hooks
When we want to share logic between two JavaScript
functions, we extract it to a third function. Both components
and Hooks are functions, so this works for them too!
A custom Hook is a JavaScript function whose name starts
with ”use” and that may call other Hooks.
Some key points
● Before there used to be mixins in react where we can share logic among components
But later it was removed because of the state we have to share between components.
● Custom hooks are somewhat like mixins but without sharing state between
components. Instead they will have their own state so it is less prone to bug.
● Can be used any number of time.
● Its name should always start with use so that you can tell at a glance that the rules of
Hooks apply to it.
Useful hooks
● Basic Hooks
■ useState
■ useEffect
■ useContext
● Additional Hooks
■ useReducer
■ useCallback
■ useMemo
■ useRef
■ useImperativeMethods
■ useLayoutEffect
useContext
Accepts a context object (the value returned from React.createContext) and returns the current
context value, as given by the nearest context provider for the given context.
When the provider updates, this Hook will trigger a rerender with the latest context value.
const context = useContext(Context);
useCallback
Returns a memoized callback.
Pass an inline callback and an array of inputs.
useCallback will return a memoized version of the
callback that only changes if one of the inputs has
changed. This is useful when passing callbacks to
optimized child components that rely on reference
equality to prevent unnecessary renders (e.g.
shouldComponentUpdate).
useRef
useRef returns a mutable ref object whose
.current property is initialized to the passed
argument (initialValue). The returned object will
persist for the full lifetime of the component.
FAQ
● Is React hook future?
○ Maybe, currently its in alpha version and soon will be in stable version.
● Will React deprecate or plan to remove class base components?
○ No! Currently React team doesn’t deprecate class base components or there are
any plan in future to remove it but encourages you to use hooks in upcoming
components.
● Do I need to rewrite all my class components?
○ No!
Thank you!

More Related Content

PDF
Basics of React Hooks.pptx.pdf
Knoldus Inc.
 
PPTX
React-JS Component Life-cycle Methods
ANKUSH CHAVAN
 
PPTX
Introduction to React JS for beginners
Varun Raj
 
PPTX
React web development
Rully Ramanda
 
PPTX
Understanding react hooks
Maulik Shah
 
PDF
React js
Rajesh Kolla
 
PDF
Introduction to Redux
Ignacio Martín
 
PPTX
Thinking in react
aashimadudeja
 
Basics of React Hooks.pptx.pdf
Knoldus Inc.
 
React-JS Component Life-cycle Methods
ANKUSH CHAVAN
 
Introduction to React JS for beginners
Varun Raj
 
React web development
Rully Ramanda
 
Understanding react hooks
Maulik Shah
 
React js
Rajesh Kolla
 
Introduction to Redux
Ignacio Martín
 
Thinking in react
aashimadudeja
 

What's hot (20)

PDF
React new features and intro to Hooks
Soluto
 
PPTX
React state
Ducat
 
PPTX
React hooks
Assaf Gannon
 
PPTX
React hooks
Ramy ElBasyouni
 
ODP
Introduction to ReactJS
Knoldus Inc.
 
PPTX
React workshop
Imran Sayed
 
PPTX
Its time to React.js
Ritesh Mehrotra
 
PDF
Important React Hooks
Knoldus Inc.
 
PPTX
ReactJS presentation.pptx
DivyanshGupta922023
 
PDF
React JS - Introduction
Sergey Romaneko
 
PPTX
React.js - The Dawn of Virtual DOM
Jimit Shah
 
PDF
React hooks Episode #1: An introduction.
ManojSatishKumar
 
PPTX
Reactjs
Neha Sharma
 
PPTX
Intro to React
Justin Reock
 
PDF
Workshop 21: React Router
Visual Engineering
 
PDF
React and redux
Mystic Coders, LLC
 
PPTX
React hooks
Sadhna Rana
 
PDF
An Introduction to Redux
NexThoughts Technologies
 
PDF
React
중운 박
 
PPTX
React + Redux Introduction
Nikolaus Graf
 
React new features and intro to Hooks
Soluto
 
React state
Ducat
 
React hooks
Assaf Gannon
 
React hooks
Ramy ElBasyouni
 
Introduction to ReactJS
Knoldus Inc.
 
React workshop
Imran Sayed
 
Its time to React.js
Ritesh Mehrotra
 
Important React Hooks
Knoldus Inc.
 
ReactJS presentation.pptx
DivyanshGupta922023
 
React JS - Introduction
Sergey Romaneko
 
React.js - The Dawn of Virtual DOM
Jimit Shah
 
React hooks Episode #1: An introduction.
ManojSatishKumar
 
Reactjs
Neha Sharma
 
Intro to React
Justin Reock
 
Workshop 21: React Router
Visual Engineering
 
React and redux
Mystic Coders, LLC
 
React hooks
Sadhna Rana
 
An Introduction to Redux
NexThoughts Technologies
 
React
중운 박
 
React + Redux Introduction
Nikolaus Graf
 
Ad

Similar to Understanding react hooks (20)

PDF
How to build a react native app with the help of react native hooks
Katy Slemon
 
PDF
Understanding React hooks | Walkingtree Technologies
Walking Tree Technologies
 
PPTX
React Hooks Best Practices in 2022.pptx
BOSC Tech Labs
 
PPTX
Green Custard Friday Talk 21: React Hooks
Green Custard
 
PDF
React state management and side-effects – A Review of Hooks
IRJET Journal
 
PDF
Fundamental concepts of react js
StephieJohn
 
PDF
100 React Interview questions 2024.pptx.pdf
codevincent624
 
PPTX
React JS; all concepts. Contains React Features, JSX, functional & Class comp...
Karmanjay Verma
 
PPTX
Redux workshop
Imran Sayed
 
PPTX
downloads_introduction to redux.pptx
NavneetKumar111924
 
PDF
React hooks beyond hype
Magdiel Duarte
 
PDF
Materi Modern React Redux Power Point.pdf
exiabreak
 
PPTX
Pluginkla2019 - React Presentation
Angela Lehru
 
PDF
unit 3_Adv WTAdvanced Web Tecg Design_HTML_CSS_JAVASCRIPT_AJAX_PPT.pdf
GauravDwivedi695361
 
PDF
Fundamental Concepts of React JS for Beginners.pdf
StephieJohn
 
PPTX
React-JS.pptx
AnmolPandita7
 
PDF
React JS & Functional Programming Principles
Andrii Lundiak
 
PDF
Redux essentials
Chandan Kumar Rana
 
PPTX
React Hooks
Erez Cohen
 
PPTX
React js programming concept
Tariqul islam
 
How to build a react native app with the help of react native hooks
Katy Slemon
 
Understanding React hooks | Walkingtree Technologies
Walking Tree Technologies
 
React Hooks Best Practices in 2022.pptx
BOSC Tech Labs
 
Green Custard Friday Talk 21: React Hooks
Green Custard
 
React state management and side-effects – A Review of Hooks
IRJET Journal
 
Fundamental concepts of react js
StephieJohn
 
100 React Interview questions 2024.pptx.pdf
codevincent624
 
React JS; all concepts. Contains React Features, JSX, functional & Class comp...
Karmanjay Verma
 
Redux workshop
Imran Sayed
 
downloads_introduction to redux.pptx
NavneetKumar111924
 
React hooks beyond hype
Magdiel Duarte
 
Materi Modern React Redux Power Point.pdf
exiabreak
 
Pluginkla2019 - React Presentation
Angela Lehru
 
unit 3_Adv WTAdvanced Web Tecg Design_HTML_CSS_JAVASCRIPT_AJAX_PPT.pdf
GauravDwivedi695361
 
Fundamental Concepts of React JS for Beginners.pdf
StephieJohn
 
React-JS.pptx
AnmolPandita7
 
React JS & Functional Programming Principles
Andrii Lundiak
 
Redux essentials
Chandan Kumar Rana
 
React Hooks
Erez Cohen
 
React js programming concept
Tariqul islam
 
Ad

Recently uploaded (20)

PPTX
Web Testing.pptx528278vshbuqffqhhqiwnwuq
studylike474
 
PPTX
oapresentation.pptx
mehatdhavalrajubhai
 
PPTX
Odoo Integration Services by Candidroot Solutions
CandidRoot Solutions Private Limited
 
PDF
Micromaid: A simple Mermaid-like chart generator for Pharo
ESUG
 
PPTX
Role Of Python In Programing Language.pptx
jaykoshti048
 
PDF
A REACT POMODORO TIMER WEB APPLICATION.pdf
Michael624841
 
PDF
Why Use Open Source Reporting Tools for Business Intelligence.pdf
Varsha Nayak
 
PPTX
PFAS Reporting Requirements 2026 Are You Submission Ready Certivo.pptx
Certivo Inc
 
PPTX
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 
PDF
Bandai Playdia The Book - David Glotz
BluePanther6
 
DOCX
The Future of Smart Factories Why Embedded Analytics Leads the Way
Varsha Nayak
 
PDF
Build Multi-agent using Agent Development Kit
FadyIbrahim23
 
PPTX
Services offered by Dynamic Solutions in Pakistan
DaniyaalAdeemShibli1
 
PPTX
The-Dawn-of-AI-Reshaping-Our-World.pptxx
parthbhanushali307
 
PDF
Microsoft Teams Essentials; The pricing and the versions_PDF.pdf
Q-Advise
 
PDF
Key Features to Look for in Arizona App Development Services
Net-Craft.com
 
PPTX
EU POPs Limits & Digital Product Passports Compliance Strategy 2025.pptx
Certivo Inc
 
PPTX
AZ900_SLA_Pricing_2025_LondonIT (1).pptx
chumairabdullahph
 
DOCX
The Five Best AI Cover Tools in 2025.docx
aivoicelabofficial
 
PPT
Order to Cash Lifecycle Overview R12 .ppt
nbvreddy229
 
Web Testing.pptx528278vshbuqffqhhqiwnwuq
studylike474
 
oapresentation.pptx
mehatdhavalrajubhai
 
Odoo Integration Services by Candidroot Solutions
CandidRoot Solutions Private Limited
 
Micromaid: A simple Mermaid-like chart generator for Pharo
ESUG
 
Role Of Python In Programing Language.pptx
jaykoshti048
 
A REACT POMODORO TIMER WEB APPLICATION.pdf
Michael624841
 
Why Use Open Source Reporting Tools for Business Intelligence.pdf
Varsha Nayak
 
PFAS Reporting Requirements 2026 Are You Submission Ready Certivo.pptx
Certivo Inc
 
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 
Bandai Playdia The Book - David Glotz
BluePanther6
 
The Future of Smart Factories Why Embedded Analytics Leads the Way
Varsha Nayak
 
Build Multi-agent using Agent Development Kit
FadyIbrahim23
 
Services offered by Dynamic Solutions in Pakistan
DaniyaalAdeemShibli1
 
The-Dawn-of-AI-Reshaping-Our-World.pptxx
parthbhanushali307
 
Microsoft Teams Essentials; The pricing and the versions_PDF.pdf
Q-Advise
 
Key Features to Look for in Arizona App Development Services
Net-Craft.com
 
EU POPs Limits & Digital Product Passports Compliance Strategy 2025.pptx
Certivo Inc
 
AZ900_SLA_Pricing_2025_LondonIT (1).pptx
chumairabdullahph
 
The Five Best AI Cover Tools in 2025.docx
aivoicelabofficial
 
Order to Cash Lifecycle Overview R12 .ppt
nbvreddy229
 

Understanding react hooks

  • 2. TOC Overview useState and uses useEffect and uses useReducer and uses Custom hooks Useful hooks FAQ Demo: https://jolly-perlman-98367f.netlify.com
  • 3. Overview Hooks are a new feature proposal that lets you use state and other React features without writing a class. They’re currently in React v16.7.0-alpha and being discussed in an open RFC
  • 4. Understanding the problems 01 Hooks lets us use state inside functional component, previously if we need state in any component we need to make class based component. 02 In functional component we were unable to use component’s life cycle hooks Like componentDidUnmount, componentDidUpdate. 03 With hooks now our functional components are no more stateless component 04 Classes confuse both people and machines, In addition to making code reuse and code organization more difficult, we’ve found that classes can be a large barrier to learning React. You have to understand how this works in JavaScript, which is very different from how it works in most languages. You have to remember to bind the event handlers.
  • 5. useState and uses Here, useState is a Hook. We call it inside a function component to add some local state to it. React will preserve this state between re-renders. useState returns a pair: the current state value and a function that lets you update it. You can call this function from an event handler or somewhere else. It’s similar to this.setState in a class, except it doesn’t merge the old and new state together. The only argument to useState is the initial state. In the example above, it is 0 because our counter starts from zero. Note that unlike this.state, the state here doesn’t have to be an object — although it can be if you want. The initial state argument is only used during the first render.
  • 6. Some key points ● we need to use useState function to have state ● We can use String, Number, Array or Object in useState ● useState return array which we destructure, which is also called array destructuring. const [state,setState] = useState('samundra') // ['samundra', function(state){ }] ● Doesn't work with class ● Doesn't work outside of component, on any vanilla javascript function description ● uses Linked List data structure ● Placement of useState matters
  • 7. useEffect and uses You’ve likely performed data fetching, subscriptions, or manually changing the DOM from React components before. We call these operations “side effects” (or “effects” for short) because they can affect other components and can’t be done during rendering. The Effect Hook, useEffect, adds the ability to perform side effects from a function component. It serves the same purpose as componentDidMount, componentDidUpdate, and componentWillUnmount in React classes, but unified into a single API.
  • 8. Some key points ● useEffect is effect which is equal to componentDidMount and ComponentDidUpdate. ● which means useEffect will run on mount and update ● will run only after the DOM is applied or DOM mutation is done. ● every time whenever a local state is changed this effect will run. ● we can make our effect run depending on other state by passing it in second argument as array ● We can make effect run only once by passing empty array in second argument
  • 9. useReducer and uses An alternative to useState. Accepts a reducer of type (state, action) => newState, and returns the current state paired with a dispatch method. (If you’re familiar with Redux, you already know how this works.)
  • 10. Some key points ● useReducer is an another hook provided by react ● when we want to manage state in single object we can use it ● Inspired by redux, we may not need redux if our app is small and we need some state management
  • 11. Some key points ● useReducer is an another hook provided by react ● when we want to manage state in single object we can use it ● Inspired by redux, we may not need redux if our app is small and we need some state management
  • 12. Custom hooks When we want to share logic between two JavaScript functions, we extract it to a third function. Both components and Hooks are functions, so this works for them too! A custom Hook is a JavaScript function whose name starts with ”use” and that may call other Hooks.
  • 13. Some key points ● Before there used to be mixins in react where we can share logic among components But later it was removed because of the state we have to share between components. ● Custom hooks are somewhat like mixins but without sharing state between components. Instead they will have their own state so it is less prone to bug. ● Can be used any number of time. ● Its name should always start with use so that you can tell at a glance that the rules of Hooks apply to it.
  • 14. Useful hooks ● Basic Hooks ■ useState ■ useEffect ■ useContext ● Additional Hooks ■ useReducer ■ useCallback ■ useMemo ■ useRef ■ useImperativeMethods ■ useLayoutEffect
  • 15. useContext Accepts a context object (the value returned from React.createContext) and returns the current context value, as given by the nearest context provider for the given context. When the provider updates, this Hook will trigger a rerender with the latest context value. const context = useContext(Context);
  • 16. useCallback Returns a memoized callback. Pass an inline callback and an array of inputs. useCallback will return a memoized version of the callback that only changes if one of the inputs has changed. This is useful when passing callbacks to optimized child components that rely on reference equality to prevent unnecessary renders (e.g. shouldComponentUpdate).
  • 17. useRef useRef returns a mutable ref object whose .current property is initialized to the passed argument (initialValue). The returned object will persist for the full lifetime of the component.
  • 18. FAQ ● Is React hook future? ○ Maybe, currently its in alpha version and soon will be in stable version. ● Will React deprecate or plan to remove class base components? ○ No! Currently React team doesn’t deprecate class base components or there are any plan in future to remove it but encourages you to use hooks in upcoming components. ● Do I need to rewrite all my class components? ○ No!