SlideShare a Scribd company logo
React patterns & hooks
 Inheritance
 Composition
React architectural methodologies
 Those familiar with Object Oriented Programming are well aware of Inheritance and use it on a
regular basis. When a child class derives properties from it’s parent class, we call it
inheritance. There are variety of use-cases where inheritance can be useful.
 Example: A car is a vehicle can be modeled with inheritance.
Inheritance
Inheritance(contd.)
 it describes a class that can reference one or more objects of another class as instances.
 Example: A car has an engine can be modeled with composition.
Composition
Composition
(contd.)
 Mixins
 Render Props
 HOC - Higher order component (container)
React patterns
Mixins
 Was introduced in 2015
 Similar to HOCs
 Syntax: React.createClass()
 Motivation:
- Code Reuse and Sharing logic
 Cons:
- Not with ES6 class but only ES5
Mixins (examples)
Mixins (Deprecated)
 Mixins are dead
- ref: https://medium.com/@dan_abramov/mixins-are-dead-long-live-higher-order-
components-94a0d2f9e750
Render Prop
Render Prop: Example
Render Prop(why to use)
 Motivation:
- Reuse code across components when using ES6 classes.
- The lowest level of indirection - it’s clear which component is called and the state
is isolated.
- No naming collision issues for props, state and class methods.
- No hoisting static methods.
Render Prop(why not to use)
 Problems:
- Caution using shouldComponentUpdate as the render prop might close over
data which is unaware of
- May cause minor memory problems when defining a closure for every render
(profiling in dev tools)
- May lead to callback hell dilemma as it can be chain of nested functions inside
each other
- renderProps callback is not so neat as it needs to be wrapped in expression
Render Prop(callback hell)
Render Prop(Profiling)
Render Prop(Libraries)
 Libraries Using renderProps:
- constate https://github.com/diegohaz/constate
- query renderer https://relay.dev/docs/en/query-renderer.html
- downshift https://github.com/downshift-js/downshift
HOC(Example)
 Function that takes another function as a parameter and returns third one
HOC(Example)
 Motivation:
- Reuse code using ES6 classes
- It is easy to make small reusable units of code, thereby supporting the single
responsibility principle.
- Apply multiple HOCs to one component by composing them. The readability can
be improve using a compose function like in Recompose.
HOC(Example)
HOC vs RenderProp
 Motivation:
- HOC better to compose over render props, especially when many cross-cutting
concerns are applied to a component. Many nested render prop components will look
similar to “callback hell”
HOC(Contd.)
 Downsides of HOC and mixins:
- boilerplate code with HOC function name
- It is easy to compose several HOCs together and then this creates a deeply
nested tree making it difficult to debug.
HOC(Contd.)
HOC(Contd.)
HOC(Contd.)
 Downsides of HOC:
- Despite ugly nesting tree, it is also hard to reference an element in HOC’s nested
tree except with react newly adopted forwardRef
React hooks
Hooks(Intro)
 Overview:
- React needs a better primitive for sharing stateful logic.
- functional stateful components
- avoiding class issues (this scoping & constructor issues)
- Hooks allow you to reuse stateful logic without changing your component
hierarchy
- render props and higher-order components that try to solve this. But these
patterns require you to restructure your components when you use them
Hooks(Contd.)
 Plan:
- 100% backwards-compatible. Hooks don’t contain any breaking changes.
- Hooks provide a more direct API to the React concepts you already know: props,
state, context, refs, and lifecycle. As we will show later, Hooks also offer a new powerful
way to combine them.
- Do not refactor old code but start hooks with new code.
Hooks(Contd.)
 Separation Motivation:
- Complex components become hard to understand so, what?
Of Course, Splitting
Hooks(Contd.)
 Separation Motivation:
-
For example, components might perform some data fetching in and . However,
the same componentDidMountmethod might also contain some unrelated logic that sets up
event listeners, with cleanup performed in componentWillUnmount. Mutually related code that
changes together gets split apart, but completely unrelated code ends up combined in a single method.
This makes it too easy to introduce bugs and inconsistencies.
Hooks(Contd.)
 Classes Motivation:
- Optimization:
For example, classes don’t minify very well, and they make hot reloading
flaky
State Hook
Multiple State Vars
UseEffect Hook
 UseEffect in class:
- Runs on every render cycle
- Similar to componentDidMount and ComponentWillUnmount and
ComponentDidUpdate
UseEffect Hook
UseEffect Hook With API
UseEffect Hook Delegate in class
component
UseEffect Hook Delegate in class
component
 Note
- It cleans up the previous effects before applying the next effects. To illustrate
this, here is a sequence of subscribe and unsubscribe calls that this component could
produce over time
UseEffect Hook Delegate in class
component (Example)
UseEffect Hook Delegate in class
component
 Note
- Forgetting to handle componentDidUpdate properly is a common source of bugs
in React applications.
- So how hooks acts on this! Let us see
UseEffect Hook With API
(Performance Consideration)
 Performance optimization:
- In this example, React would unsubscribe from our ChatAPI when the component unmounts,
as well as before re-running the effect due to a subsequent render
(If you want, there’s a way to tell React to skip re-subscribing if the props.friend.id we passed to
ChatAPI didn’t change.)
UseEffect Hook With API
(Performance Consideration)
In this example, React would unsubscribe from our ChatAPI when the component unmounts,
as well as before re-running the effect due to a subsequent render
(If you want, there’s a way to tell React to skip re-subscribing if the props.friend.id we passed to
ChatAPI didn’t change.)
UseEffect Hook With API
(Performance Consideration)
UseEffect (rendering once)
UseEffect Hook With API
(Performance Consideration)
UseCallback hook
 Memoization:
- Memoized callbacks
UseCallback hook
UseCallback Hook
 Memoization:
-
-
Pass an inline callback and an array of dependencies. useCallback will return a memoized version of
the callback that only changes if one of the dependencies has changed.
This is useful when passing callbacks to optimized child components that rely on reference equality
to prevent unnecessary renders (e.g. shouldComponentUpdate).
useCallback(fn, deps) is equivalent to useMemo(() => fn, deps).
Update state with complex logic
 userReducer Hook:
- manage complex state update logic
- manage state if separates on previous state
- separation of concerns
userReducer (contd.)
Delegate to instance variables
(useRef)
 useRef Hook:
- instance varaiables in class are useRef in hooks
- mutable varaiables that changes over time
- avoiding unneeded hook rerender
useRef (Example)
Classes to hooks
Rules of using hooks
 Rules:
- Do not use hooks inside loops or conditions – this ensures the state of this
hook between multiple useState and useEffect calls
- Do not call hooks inside regular Js function
- Call hook from functional component
- Call hook from custom hook
References
 References:
- https://programmingwithmosh.com/react/react-composition-vs-inheritance/
- https://programmingwithmosh.com/javascript/react-lifecycle-methods/
- https://reactpatterns.com/
- https://www.richardkotze.com/coding/understanding-render-props-react-js
- https://www.richardkotze.com/coding/hoc-vs-render-props-react
- https://cdb.reacttraining.com/react-inline-functions-and-performance-
bdff784f5578
 References:
- https://cdb.reacttraining.com/use-a-render-prop-50de598f11ce => treasure
HOC: hoisting static methods https://medium.com/@peterpme/learning-higher-order-
components-in-react-by-building-a-loading-screen-9f705b89f569
- https://egghead.io/lessons/react-handle-static-properties-properly-with-higher-
order-components
- https://github.com/facebookincubator/redux-react-hook => using redux with
hooks
- https://blog.logrocket.com/how-to-migrate-from-hocs-to-hooks-d0f7675fd600
- https://reactjs.org/docs/hooks-rules.html
- https://reactjs.org/docs/hooks-custom.html => custom hooks
- https://reactjs.org/docs/hooks-faq.html#how-do-i-implement-
shouldcomponentupdate
References (contd.)
Rules of using hooks
Questions !!
Ad

More Related Content

What's hot (20)

React hooks
React hooksReact hooks
React hooks
Assaf Gannon
 
React Hooks
React HooksReact Hooks
React Hooks
Joao Marins
 
Introduction to ReactJS
Introduction to ReactJSIntroduction to ReactJS
Introduction to ReactJS
Knoldus Inc.
 
reactJS
reactJSreactJS
reactJS
Syam Santhosh
 
An introduction to React.js
An introduction to React.jsAn introduction to React.js
An introduction to React.js
Emanuele DelBono
 
React workshop
React workshopReact workshop
React workshop
Imran Sayed
 
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
 
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
 
introduction to Vue.js 3
introduction to Vue.js 3 introduction to Vue.js 3
introduction to Vue.js 3
ArezooKmn
 
React Hooks
React HooksReact Hooks
React Hooks
Erez Cohen
 
[Final] ReactJS presentation
[Final] ReactJS presentation[Final] ReactJS presentation
[Final] ReactJS presentation
洪 鹏发
 
Asynchronous JavaScript Programming with Callbacks & Promises
Asynchronous JavaScript Programming with Callbacks & PromisesAsynchronous JavaScript Programming with Callbacks & Promises
Asynchronous JavaScript Programming with Callbacks & Promises
Hùng Nguyễn Huy
 
React + Redux Introduction
React + Redux IntroductionReact + Redux Introduction
React + Redux Introduction
Nikolaus Graf
 
Intro to React
Intro to ReactIntro to React
Intro to React
Eric Westfall
 
How to implement internationalization (i18n) in angular application(multiple ...
How to implement internationalization (i18n) in angular application(multiple ...How to implement internationalization (i18n) in angular application(multiple ...
How to implement internationalization (i18n) in angular application(multiple ...
Katy Slemon
 
Its time to React.js
Its time to React.jsIts time to React.js
Its time to React.js
Ritesh Mehrotra
 
React hooks
React hooksReact hooks
React hooks
Sadhna Rana
 
Intro to React
Intro to ReactIntro to React
Intro to React
Justin Reock
 
ReactJS
ReactJSReactJS
ReactJS
Hiten Pratap Singh
 
Microservices with Java, Spring Boot and Spring Cloud
Microservices with Java, Spring Boot and Spring CloudMicroservices with Java, Spring Boot and Spring Cloud
Microservices with Java, Spring Boot and Spring Cloud
Eberhard Wolff
 
Introduction to ReactJS
Introduction to ReactJSIntroduction to ReactJS
Introduction to ReactJS
Knoldus Inc.
 
An introduction to React.js
An introduction to React.jsAn introduction to React.js
An introduction to React.js
Emanuele DelBono
 
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
 
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
 
introduction to Vue.js 3
introduction to Vue.js 3 introduction to Vue.js 3
introduction to Vue.js 3
ArezooKmn
 
[Final] ReactJS presentation
[Final] ReactJS presentation[Final] ReactJS presentation
[Final] ReactJS presentation
洪 鹏发
 
Asynchronous JavaScript Programming with Callbacks & Promises
Asynchronous JavaScript Programming with Callbacks & PromisesAsynchronous JavaScript Programming with Callbacks & Promises
Asynchronous JavaScript Programming with Callbacks & Promises
Hùng Nguyễn Huy
 
React + Redux Introduction
React + Redux IntroductionReact + Redux Introduction
React + Redux Introduction
Nikolaus Graf
 
How to implement internationalization (i18n) in angular application(multiple ...
How to implement internationalization (i18n) in angular application(multiple ...How to implement internationalization (i18n) in angular application(multiple ...
How to implement internationalization (i18n) in angular application(multiple ...
Katy Slemon
 
Microservices with Java, Spring Boot and Spring Cloud
Microservices with Java, Spring Boot and Spring CloudMicroservices with Java, Spring Boot and Spring Cloud
Microservices with Java, Spring Boot and Spring Cloud
Eberhard Wolff
 

Similar to React hooks (20)

ReactJS.pptx
ReactJS.pptxReactJS.pptx
ReactJS.pptx
SamyakShetty2
 
Corso su ReactJS
Corso su ReactJSCorso su ReactJS
Corso su ReactJS
LinkMe Srl
 
A React Journey
A React JourneyA React Journey
A React Journey
LinkMe Srl
 
Using advanced C# features in Sharepoint development
Using advanced C# features in Sharepoint developmentUsing advanced C# features in Sharepoint development
Using advanced C# features in Sharepoint development
sadomovalex
 
Patterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docxPatterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docx
danhaley45372
 
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
 
Warsaw Frontend Meetup #1 - Webpack
Warsaw Frontend Meetup #1 - WebpackWarsaw Frontend Meetup #1 - Webpack
Warsaw Frontend Meetup #1 - Webpack
Radosław Rosłaniec
 
react-en.pdf
react-en.pdfreact-en.pdf
react-en.pdf
ssuser65180a
 
React gsg presentation with ryan jung & elias malik
React   gsg presentation with ryan jung & elias malikReact   gsg presentation with ryan jung & elias malik
React gsg presentation with ryan jung & elias malik
Lama K Banna
 
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
Green Custard
 
React & Redux JS
React & Redux JS React & Redux JS
React & Redux JS
Hamed Farag
 
P Training Presentation
P Training PresentationP Training Presentation
P Training Presentation
Gaurav Tyagi
 
Fundamental concepts of react js
Fundamental concepts of react jsFundamental concepts of react js
Fundamental concepts of react js
StephieJohn
 
Designing Better API
Designing Better APIDesigning Better API
Designing Better API
Kaniska Mandal
 
React
ReactReact
React
manii kanta
 
React-JS.pptx
React-JS.pptxReact-JS.pptx
React-JS.pptx
AnmolPandita7
 
React Native +Redux + ES6 (Updated)
React Native +Redux + ES6 (Updated)React Native +Redux + ES6 (Updated)
React Native +Redux + ES6 (Updated)
Chiew Carol
 
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
BOSC Tech Labs
 
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 Best Practices All Developers Should Follow in 2024.pdf
React Best Practices All Developers Should Follow in 2024.pdfReact Best Practices All Developers Should Follow in 2024.pdf
React Best Practices All Developers Should Follow in 2024.pdf
BOSC Tech Labs
 
Corso su ReactJS
Corso su ReactJSCorso su ReactJS
Corso su ReactJS
LinkMe Srl
 
A React Journey
A React JourneyA React Journey
A React Journey
LinkMe Srl
 
Using advanced C# features in Sharepoint development
Using advanced C# features in Sharepoint developmentUsing advanced C# features in Sharepoint development
Using advanced C# features in Sharepoint development
sadomovalex
 
Patterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docxPatterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docx
danhaley45372
 
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
 
Warsaw Frontend Meetup #1 - Webpack
Warsaw Frontend Meetup #1 - WebpackWarsaw Frontend Meetup #1 - Webpack
Warsaw Frontend Meetup #1 - Webpack
Radosław Rosłaniec
 
React gsg presentation with ryan jung & elias malik
React   gsg presentation with ryan jung & elias malikReact   gsg presentation with ryan jung & elias malik
React gsg presentation with ryan jung & elias malik
Lama K Banna
 
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
Green Custard
 
React & Redux JS
React & Redux JS React & Redux JS
React & Redux JS
Hamed Farag
 
P Training Presentation
P Training PresentationP Training Presentation
P Training Presentation
Gaurav Tyagi
 
Fundamental concepts of react js
Fundamental concepts of react jsFundamental concepts of react js
Fundamental concepts of react js
StephieJohn
 
React Native +Redux + ES6 (Updated)
React Native +Redux + ES6 (Updated)React Native +Redux + ES6 (Updated)
React Native +Redux + ES6 (Updated)
Chiew Carol
 
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
BOSC Tech Labs
 
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 Best Practices All Developers Should Follow in 2024.pdf
React Best Practices All Developers Should Follow in 2024.pdfReact Best Practices All Developers Should Follow in 2024.pdf
React Best Practices All Developers Should Follow in 2024.pdf
BOSC Tech Labs
 
Ad

Recently uploaded (20)

Cyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of securityCyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of security
riccardosl1
 
Build Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For DevsBuild Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For Devs
Brian McKeiver
 
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
BookNet Canada
 
Greenhouse_Monitoring_Presentation.pptx.
Greenhouse_Monitoring_Presentation.pptx.Greenhouse_Monitoring_Presentation.pptx.
Greenhouse_Monitoring_Presentation.pptx.
hpbmnnxrvb
 
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptxDevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
Justin Reock
 
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdfComplete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Software Company
 
Procurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptxProcurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptx
Jon Hansen
 
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
Alan Dix
 
AI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global TrendsAI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global Trends
InData Labs
 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
SOFTTECHHUB
 
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
organizerofv
 
Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)
Ortus Solutions, Corp
 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In FranceManifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
chb3
 
Technology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data AnalyticsTechnology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data Analytics
InData Labs
 
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc
 
Role of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered ManufacturingRole of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered Manufacturing
Andrew Leo
 
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Aqusag Technologies
 
Rusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond SparkRusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond Spark
carlyakerly1
 
Cyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of securityCyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of security
riccardosl1
 
Build Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For DevsBuild Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For Devs
Brian McKeiver
 
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
BookNet Canada
 
Greenhouse_Monitoring_Presentation.pptx.
Greenhouse_Monitoring_Presentation.pptx.Greenhouse_Monitoring_Presentation.pptx.
Greenhouse_Monitoring_Presentation.pptx.
hpbmnnxrvb
 
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptxDevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
Justin Reock
 
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdfComplete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Software Company
 
Procurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptxProcurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptx
Jon Hansen
 
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
Alan Dix
 
AI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global TrendsAI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global Trends
InData Labs
 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
SOFTTECHHUB
 
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
organizerofv
 
Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)
Ortus Solutions, Corp
 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In FranceManifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
chb3
 
Technology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data AnalyticsTechnology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data Analytics
InData Labs
 
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc
 
Role of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered ManufacturingRole of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered Manufacturing
Andrew Leo
 
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Aqusag Technologies
 
Rusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond SparkRusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond Spark
carlyakerly1
 
Ad

React hooks

  • 2.  Inheritance  Composition React architectural methodologies
  • 3.  Those familiar with Object Oriented Programming are well aware of Inheritance and use it on a regular basis. When a child class derives properties from it’s parent class, we call it inheritance. There are variety of use-cases where inheritance can be useful.  Example: A car is a vehicle can be modeled with inheritance. Inheritance
  • 5.  it describes a class that can reference one or more objects of another class as instances.  Example: A car has an engine can be modeled with composition. Composition
  • 7.  Mixins  Render Props  HOC - Higher order component (container) React patterns
  • 8. Mixins  Was introduced in 2015  Similar to HOCs  Syntax: React.createClass()  Motivation: - Code Reuse and Sharing logic  Cons: - Not with ES6 class but only ES5
  • 10. Mixins (Deprecated)  Mixins are dead - ref: https://medium.com/@dan_abramov/mixins-are-dead-long-live-higher-order- components-94a0d2f9e750
  • 13. Render Prop(why to use)  Motivation: - Reuse code across components when using ES6 classes. - The lowest level of indirection - it’s clear which component is called and the state is isolated. - No naming collision issues for props, state and class methods. - No hoisting static methods.
  • 14. Render Prop(why not to use)  Problems: - Caution using shouldComponentUpdate as the render prop might close over data which is unaware of - May cause minor memory problems when defining a closure for every render (profiling in dev tools) - May lead to callback hell dilemma as it can be chain of nested functions inside each other - renderProps callback is not so neat as it needs to be wrapped in expression
  • 17. Render Prop(Libraries)  Libraries Using renderProps: - constate https://github.com/diegohaz/constate - query renderer https://relay.dev/docs/en/query-renderer.html - downshift https://github.com/downshift-js/downshift
  • 18. HOC(Example)  Function that takes another function as a parameter and returns third one
  • 19. HOC(Example)  Motivation: - Reuse code using ES6 classes - It is easy to make small reusable units of code, thereby supporting the single responsibility principle. - Apply multiple HOCs to one component by composing them. The readability can be improve using a compose function like in Recompose.
  • 21. HOC vs RenderProp  Motivation: - HOC better to compose over render props, especially when many cross-cutting concerns are applied to a component. Many nested render prop components will look similar to “callback hell”
  • 22. HOC(Contd.)  Downsides of HOC and mixins: - boilerplate code with HOC function name - It is easy to compose several HOCs together and then this creates a deeply nested tree making it difficult to debug.
  • 25. HOC(Contd.)  Downsides of HOC: - Despite ugly nesting tree, it is also hard to reference an element in HOC’s nested tree except with react newly adopted forwardRef
  • 27. Hooks(Intro)  Overview: - React needs a better primitive for sharing stateful logic. - functional stateful components - avoiding class issues (this scoping & constructor issues) - Hooks allow you to reuse stateful logic without changing your component hierarchy - render props and higher-order components that try to solve this. But these patterns require you to restructure your components when you use them
  • 28. Hooks(Contd.)  Plan: - 100% backwards-compatible. Hooks don’t contain any breaking changes. - Hooks provide a more direct API to the React concepts you already know: props, state, context, refs, and lifecycle. As we will show later, Hooks also offer a new powerful way to combine them. - Do not refactor old code but start hooks with new code.
  • 29. Hooks(Contd.)  Separation Motivation: - Complex components become hard to understand so, what? Of Course, Splitting
  • 30. Hooks(Contd.)  Separation Motivation: - For example, components might perform some data fetching in and . However, the same componentDidMountmethod might also contain some unrelated logic that sets up event listeners, with cleanup performed in componentWillUnmount. Mutually related code that changes together gets split apart, but completely unrelated code ends up combined in a single method. This makes it too easy to introduce bugs and inconsistencies.
  • 31. Hooks(Contd.)  Classes Motivation: - Optimization: For example, classes don’t minify very well, and they make hot reloading flaky
  • 34. UseEffect Hook  UseEffect in class: - Runs on every render cycle - Similar to componentDidMount and ComponentWillUnmount and ComponentDidUpdate
  • 37. UseEffect Hook Delegate in class component
  • 38. UseEffect Hook Delegate in class component  Note - It cleans up the previous effects before applying the next effects. To illustrate this, here is a sequence of subscribe and unsubscribe calls that this component could produce over time
  • 39. UseEffect Hook Delegate in class component (Example)
  • 40. UseEffect Hook Delegate in class component  Note - Forgetting to handle componentDidUpdate properly is a common source of bugs in React applications. - So how hooks acts on this! Let us see
  • 41. UseEffect Hook With API (Performance Consideration)  Performance optimization: - In this example, React would unsubscribe from our ChatAPI when the component unmounts, as well as before re-running the effect due to a subsequent render (If you want, there’s a way to tell React to skip re-subscribing if the props.friend.id we passed to ChatAPI didn’t change.)
  • 42. UseEffect Hook With API (Performance Consideration) In this example, React would unsubscribe from our ChatAPI when the component unmounts, as well as before re-running the effect due to a subsequent render (If you want, there’s a way to tell React to skip re-subscribing if the props.friend.id we passed to ChatAPI didn’t change.)
  • 43. UseEffect Hook With API (Performance Consideration)
  • 45. UseEffect Hook With API (Performance Consideration)
  • 48. UseCallback Hook  Memoization: - - Pass an inline callback and an array of dependencies. useCallback will return a memoized version of the callback that only changes if one of the dependencies has changed. This is useful when passing callbacks to optimized child components that rely on reference equality to prevent unnecessary renders (e.g. shouldComponentUpdate). useCallback(fn, deps) is equivalent to useMemo(() => fn, deps).
  • 49. Update state with complex logic  userReducer Hook: - manage complex state update logic - manage state if separates on previous state - separation of concerns
  • 51. Delegate to instance variables (useRef)  useRef Hook: - instance varaiables in class are useRef in hooks - mutable varaiables that changes over time - avoiding unneeded hook rerender
  • 54. Rules of using hooks  Rules: - Do not use hooks inside loops or conditions – this ensures the state of this hook between multiple useState and useEffect calls - Do not call hooks inside regular Js function - Call hook from functional component - Call hook from custom hook
  • 55. References  References: - https://programmingwithmosh.com/react/react-composition-vs-inheritance/ - https://programmingwithmosh.com/javascript/react-lifecycle-methods/ - https://reactpatterns.com/ - https://www.richardkotze.com/coding/understanding-render-props-react-js - https://www.richardkotze.com/coding/hoc-vs-render-props-react - https://cdb.reacttraining.com/react-inline-functions-and-performance- bdff784f5578
  • 56.  References: - https://cdb.reacttraining.com/use-a-render-prop-50de598f11ce => treasure HOC: hoisting static methods https://medium.com/@peterpme/learning-higher-order- components-in-react-by-building-a-loading-screen-9f705b89f569 - https://egghead.io/lessons/react-handle-static-properties-properly-with-higher- order-components - https://github.com/facebookincubator/redux-react-hook => using redux with hooks - https://blog.logrocket.com/how-to-migrate-from-hocs-to-hooks-d0f7675fd600 - https://reactjs.org/docs/hooks-rules.html - https://reactjs.org/docs/hooks-custom.html => custom hooks - https://reactjs.org/docs/hooks-faq.html#how-do-i-implement- shouldcomponentupdate References (contd.)
  • 57. Rules of using hooks Questions !!