SlideShare a Scribd company logo
1 of 19
Download to read offline
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

What's hot (20)

React js programming concept
React js programming conceptReact js programming concept
React js programming concept
 
React js
React jsReact js
React js
 
React-JS Component Life-cycle Methods
React-JS Component Life-cycle MethodsReact-JS Component Life-cycle Methods
React-JS Component Life-cycle Methods
 
React and redux
React and reduxReact and redux
React and redux
 
React js for beginners
React js for beginnersReact js for beginners
React js for beginners
 
Introduction to Redux
Introduction to ReduxIntroduction to Redux
Introduction to Redux
 
React state
React  stateReact  state
React state
 
Learn react-js
Learn react-jsLearn react-js
Learn react-js
 
An Introduction to Redux
An Introduction to ReduxAn Introduction to Redux
An Introduction to Redux
 
Reactjs
Reactjs Reactjs
Reactjs
 
Introduction to React JS for beginners
Introduction to React JS for beginners Introduction to React JS for beginners
Introduction to React JS for beginners
 
Redux workshop
Redux workshopRedux workshop
Redux workshop
 
JavaScript Promises
JavaScript PromisesJavaScript Promises
JavaScript Promises
 
React hooks Episode #1: An introduction.
React hooks Episode #1: An introduction.React hooks Episode #1: An introduction.
React hooks Episode #1: An 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
 
Intro to Asynchronous Javascript
Intro to Asynchronous JavascriptIntro to Asynchronous Javascript
Intro to Asynchronous Javascript
 
Introduction to React JS
Introduction to React JSIntroduction to React JS
Introduction to React JS
 
NodeJS for Beginner
NodeJS for BeginnerNodeJS for Beginner
NodeJS for Beginner
 
Angular & RXJS: examples and use cases
Angular & RXJS: examples and use casesAngular & RXJS: examples and use cases
Angular & RXJS: examples and use cases
 
Introduction to JSX
Introduction to JSXIntroduction to JSX
Introduction to JSX
 

Similar to Understanding react hooks

How to build a react native app with the help of react native hooks
How to build a react native app with the help of react native hooksHow to build a react native app with the help of react native hooks
How to build a react native app with the help of react native hooksKaty Slemon
 
Important React Hooks
Important React HooksImportant React Hooks
Important React HooksKnoldus Inc.
 
React Hooks Best Practices in 2022.pptx
React Hooks Best Practices in 2022.pptxReact Hooks Best Practices in 2022.pptx
React Hooks Best Practices in 2022.pptxBOSC Tech Labs
 
Green Custard Friday Talk 21: React Hooks
Green Custard Friday Talk 21: React HooksGreen Custard Friday Talk 21: React Hooks
Green Custard Friday Talk 21: React HooksGreen Custard
 
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 HooksIRJET Journal
 
Fundamental concepts of react js
Fundamental concepts of react jsFundamental concepts of react js
Fundamental concepts of react jsStephieJohn
 
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
 
downloads_introduction to redux.pptx
downloads_introduction to redux.pptxdownloads_introduction to redux.pptx
downloads_introduction to redux.pptxNavneetKumar111924
 
React hooks beyond hype
React hooks beyond hypeReact hooks beyond hype
React hooks beyond hypeMagdiel Duarte
 
Pluginkla2019 - React Presentation
Pluginkla2019 - React PresentationPluginkla2019 - React Presentation
Pluginkla2019 - React PresentationAngela Lehru
 
Fundamental Concepts of React JS for Beginners.pdf
Fundamental Concepts of React JS for Beginners.pdfFundamental Concepts of React JS for Beginners.pdf
Fundamental Concepts of React JS for Beginners.pdfStephieJohn
 
React JS & Functional Programming Principles
React JS & Functional Programming PrinciplesReact JS & Functional Programming Principles
React JS & Functional Programming PrinciplesAndrii Lundiak
 
Redux and context api with react native app introduction, use cases, implemen...
Redux and context api with react native app introduction, use cases, implemen...Redux and context api with react native app introduction, use cases, implemen...
Redux and context api with react native app introduction, use cases, implemen...Katy Slemon
 
React JS Interview Question & Answer
React JS Interview Question & AnswerReact JS Interview Question & Answer
React JS Interview Question & AnswerMildain Solutions
 
an Introduction to Redux
an Introduction to Reduxan Introduction to Redux
an Introduction to ReduxAmin Ashtiani
 

Similar to Understanding react hooks (20)

How to build a react native app with the help of react native hooks
How to build a react native app with the help of react native hooksHow to build a react native app with the help of react native hooks
How to build a react native app with the help of react native hooks
 
Important React Hooks
Important React HooksImportant React Hooks
Important React Hooks
 
React Hooks Best Practices in 2022.pptx
React Hooks Best Practices in 2022.pptxReact Hooks Best Practices in 2022.pptx
React Hooks Best Practices in 2022.pptx
 
Green Custard Friday Talk 21: React Hooks
Green Custard Friday Talk 21: React HooksGreen Custard Friday Talk 21: React Hooks
Green Custard Friday Talk 21: React Hooks
 
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
 
Fundamental concepts of react js
Fundamental concepts of react jsFundamental concepts of react js
Fundamental concepts of react js
 
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...
 
downloads_introduction to redux.pptx
downloads_introduction to redux.pptxdownloads_introduction to redux.pptx
downloads_introduction to redux.pptx
 
React hooks beyond hype
React hooks beyond hypeReact hooks beyond hype
React hooks beyond hype
 
Pluginkla2019 - React Presentation
Pluginkla2019 - React PresentationPluginkla2019 - React Presentation
Pluginkla2019 - React Presentation
 
Fundamental Concepts of React JS for Beginners.pdf
Fundamental Concepts of React JS for Beginners.pdfFundamental Concepts of React JS for Beginners.pdf
Fundamental Concepts of React JS for Beginners.pdf
 
React-JS.pptx
React-JS.pptxReact-JS.pptx
React-JS.pptx
 
React JS & Functional Programming Principles
React JS & Functional Programming PrinciplesReact JS & Functional Programming Principles
React JS & Functional Programming Principles
 
Redux essentials
Redux essentialsRedux essentials
Redux essentials
 
Redux and context api with react native app introduction, use cases, implemen...
Redux and context api with react native app introduction, use cases, implemen...Redux and context api with react native app introduction, use cases, implemen...
Redux and context api with react native app introduction, use cases, implemen...
 
React
ReactReact
React
 
Simple React Todo List
Simple React Todo ListSimple React Todo List
Simple React Todo List
 
React JS Interview Question & Answer
React JS Interview Question & AnswerReact JS Interview Question & Answer
React JS Interview Question & Answer
 
The Road To Redux
The Road To ReduxThe Road To Redux
The Road To Redux
 
an Introduction to Redux
an Introduction to Reduxan Introduction to Redux
an Introduction to Redux
 

Recently uploaded

Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....kzayra69
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfStefano Stabellini
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Matt Ray
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noidabntitsolutionsrishis
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfFerryKemperman
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 

Recently uploaded (20)

Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdf
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdf
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 

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!