SlideShare a Scribd company logo
4
Most read
5
Most read
8
Most read
React Context API
2
NodeXperts
Overview
● What is React Context API
● When to use Context
● How to use React Context API
○ React.createContext
○ Context.Provider
○ Context.Consumer
● Examples
● Resources
● Q&A
3
NodeXperts
What is React Context API
“The Context API is a component structure provided by the React
framework, which enables us to share specific forms of data across all
levels of the application. It’s aimed at solving the problem of prop drilling.”
4
NodeXperts
Before Context API
Problem
● Prop Drilling
● We pass props down to the levels and levels of the
component tree when not all of those components
necessarily need those props.
● Suppose component hierarchy is complex then
state management would be overhead for
developers.
5
NodeXperts
After Context API
Solution
● For state management there are couple of libraries
available like Redux (most used and trending).
● But React introduced the Context API to solve the
problem of props drilling and made developers work of
state management simple.
6
NodeXperts
When to Use
When to use Context
● As React suggests “If you only want to avoid passing
some props through many levels.
● Context is designed to share data that can be
considered “global” for a tree of React components.
● Such as the current authenticated user, theme, or
preferred language.”
7
NodeXperts
How to Use React Context API
8
NodeXperts
Context.Provider
Every Context object comes with a Provider React
component that allows consuming components to
subscribe to context changes.
<MyContext.Provider value={/* some value */}>
9
NodeXperts
Context.Consumer
A React component that subscribes to context changes. This lets
you subscribe to a context within a function component.
<MyContext.Consumer>
{value => /* render something based on the context value */}
</MyContext.Consumer>
10
NodeXperts
Example
● Dynamic Context
● Updating Context
● Multiple Context
11
NodeXperts
12
NodeXperts
Class.contextType
class MyClass extends React.Component {
componentDidMount() {
let value = this.context;
/* perform a side-effect at mount using the value of MyContext */
}
componentDidUpdate() {
let value = this.context;
/* ... */
}
componentWillUnmount() {
let value = this.context;
/* ... */
}
render() {
let value = this.context;
/* render something based on the value of MyContext */
}
}
MyClass.contextType = MyContext;
13
NodeXperts
React.createContext
Creates a Context object. When React renders a
component that subscribes to this Context object it will
read the current context value from the closest
matching Provider above it in the tree.
○ const MyContext =
React.createContext(defaultValue)
14
NodeXperts
Resources
● https://reactjs.org/docs/context.html
● https://www.toptal.com/react/react-context-api
● https://appdividend.com/2018/11/03/react-context-
api-tutorial-with-example/
● https://blog.pusher.com/react-context-api/
15
NodeXperts
Q1 . Can Context replace Redux?
Ans. Yeah context can replace Redux,
but it won’t do everything that Redux does. In particular, it
won’t perform any of the optimizations that Redux gives you
for free. So before you replace Redux with context, there’s a
thing or two that you should know about performance.
● Redux provides time traveller debugging
● It provides debugging tools
● It provides middlewares
● It combines different reducers they don’t affect each other
Questions and Answers
16
NodeXperts
Reach me at
visaali.s@successive.tech

More Related Content

What's hot (20)

PPTX
Its time to React.js
Ritesh Mehrotra
 
PPTX
React hooks
Ramy ElBasyouni
 
PPTX
React + Redux Introduction
Nikolaus Graf
 
PDF
React js
Rajesh Kolla
 
PPTX
React workshop
Imran Sayed
 
PPTX
Reactjs
Neha Sharma
 
PPTX
Intro to React
Justin Reock
 
PDF
Introduction to React JS
Bethmi Gunasekara
 
PPTX
React js
Oswald Campesato
 
PPTX
React JS - A quick introduction tutorial
Mohammed Fazuluddin
 
PDF
ReactJS presentation
Thanh Tuong
 
PDF
WEB DEVELOPMENT USING REACT JS
MuthuKumaran Singaravelu
 
PDF
An introduction to React.js
Emanuele DelBono
 
PDF
React JS - Introduction
Sergey Romaneko
 
PDF
An Introduction to ReactJS
All Things Open
 
PDF
Understanding react hooks
Samundra khatri
 
PPTX
React js programming concept
Tariqul islam
 
PPTX
Understanding react hooks
Maulik Shah
 
PDF
Introduction to Redux
Ignacio Martín
 
Its time to React.js
Ritesh Mehrotra
 
React hooks
Ramy ElBasyouni
 
React + Redux Introduction
Nikolaus Graf
 
React js
Rajesh Kolla
 
React workshop
Imran Sayed
 
Reactjs
Neha Sharma
 
Intro to React
Justin Reock
 
Introduction to React JS
Bethmi Gunasekara
 
React JS - A quick introduction tutorial
Mohammed Fazuluddin
 
ReactJS presentation
Thanh Tuong
 
WEB DEVELOPMENT USING REACT JS
MuthuKumaran Singaravelu
 
An introduction to React.js
Emanuele DelBono
 
React JS - Introduction
Sergey Romaneko
 
An Introduction to ReactJS
All Things Open
 
Understanding react hooks
Samundra khatri
 
React js programming concept
Tariqul islam
 
Understanding react hooks
Maulik Shah
 
Introduction to Redux
Ignacio Martín
 

Similar to React Context API (20)

PDF
Redux and context api with react native app introduction, use cases, implemen...
Katy Slemon
 
PPTX
what is context API and How it works in React.pptx
BOSC Tech Labs
 
PDF
Content-Driven Apps with React
Netcetera
 
PPTX
React JS .NET
Jennifer Estrada
 
PPTX
Intro react js
Vijayakanth MP
 
PPTX
Reactjs notes.pptx for web development- tutorial and theory
jobinThomas54
 
PPTX
reactJS
Syam Santhosh
 
PPTX
Redux workshop
Imran Sayed
 
PDF
Fundamental concepts of react js
StephieJohn
 
PPTX
How To Utilize Context API With Class And Functional Componen in React.pptx
BOSC Tech Labs
 
PPTX
React + Redux + TypeScript === ♥
Remo Jansen
 
PDF
A React Journey
LinkMe Srl
 
PDF
React Redux Interview Questions PDF By ScholarHat
Scholarhat
 
PDF
React Redux Interview Questions PDF By ScholarHat
Scholarhat
 
PPTX
react-slides.pptx
DayNightGaMiNg
 
PPTX
Kubernetes #1 intro
Terry Cho
 
PPTX
react-slidlkjfl;kj;dlkjopidfjhopijgpoerjpofjiwoepifjopweifjepoies.pptx
PrathamSharma77833
 
PDF
react-slides.pdf
DayNightGaMiNg
 
PDF
react-slides.pdf gives information about react library
janet736113
 
PPTX
How To Upgrade The React 18 Release Candidate.pptx
BOSC Tech Labs
 
Redux and context api with react native app introduction, use cases, implemen...
Katy Slemon
 
what is context API and How it works in React.pptx
BOSC Tech Labs
 
Content-Driven Apps with React
Netcetera
 
React JS .NET
Jennifer Estrada
 
Intro react js
Vijayakanth MP
 
Reactjs notes.pptx for web development- tutorial and theory
jobinThomas54
 
reactJS
Syam Santhosh
 
Redux workshop
Imran Sayed
 
Fundamental concepts of react js
StephieJohn
 
How To Utilize Context API With Class And Functional Componen in React.pptx
BOSC Tech Labs
 
React + Redux + TypeScript === ♥
Remo Jansen
 
A React Journey
LinkMe Srl
 
React Redux Interview Questions PDF By ScholarHat
Scholarhat
 
React Redux Interview Questions PDF By ScholarHat
Scholarhat
 
react-slides.pptx
DayNightGaMiNg
 
Kubernetes #1 intro
Terry Cho
 
react-slidlkjfl;kj;dlkjopidfjhopijgpoerjpofjiwoepifjopweifjepoies.pptx
PrathamSharma77833
 
react-slides.pdf
DayNightGaMiNg
 
react-slides.pdf gives information about react library
janet736113
 
How To Upgrade The React 18 Release Candidate.pptx
BOSC Tech Labs
 
Ad

More from NodeXperts (20)

PDF
ECMA Script
NodeXperts
 
PDF
Apollo Server IV
NodeXperts
 
PDF
Devops - Microservice and Kubernetes
NodeXperts
 
PDF
Introduction to EC2 (AWS)
NodeXperts
 
PDF
Reactive Application Using METEOR
NodeXperts
 
PDF
Apollo server II
NodeXperts
 
PDF
Apollo Server
NodeXperts
 
PDF
Apollo Server III
NodeXperts
 
PPTX
Getting Reactive Data
NodeXperts
 
PPTX
State, Life cycle, Methods & Events
NodeXperts
 
PPTX
Refs in react
NodeXperts
 
PPTX
Flow router, components and props
NodeXperts
 
PPTX
Using react with meteor
NodeXperts
 
PPTX
Introduction to Reactjs
NodeXperts
 
PPTX
Mobile apps using meteor - Part 1
NodeXperts
 
PPTX
Microservice architecture : Part 1
NodeXperts
 
PPTX
Reactive web applications using MeteorJS
NodeXperts
 
PPTX
Improving build solutions dependency management with webpack
NodeXperts
 
PPTX
Meteor workshop
NodeXperts
 
PPTX
Introduction to meteor
NodeXperts
 
ECMA Script
NodeXperts
 
Apollo Server IV
NodeXperts
 
Devops - Microservice and Kubernetes
NodeXperts
 
Introduction to EC2 (AWS)
NodeXperts
 
Reactive Application Using METEOR
NodeXperts
 
Apollo server II
NodeXperts
 
Apollo Server
NodeXperts
 
Apollo Server III
NodeXperts
 
Getting Reactive Data
NodeXperts
 
State, Life cycle, Methods & Events
NodeXperts
 
Refs in react
NodeXperts
 
Flow router, components and props
NodeXperts
 
Using react with meteor
NodeXperts
 
Introduction to Reactjs
NodeXperts
 
Mobile apps using meteor - Part 1
NodeXperts
 
Microservice architecture : Part 1
NodeXperts
 
Reactive web applications using MeteorJS
NodeXperts
 
Improving build solutions dependency management with webpack
NodeXperts
 
Meteor workshop
NodeXperts
 
Introduction to meteor
NodeXperts
 
Ad

Recently uploaded (20)

PPTX
西班牙巴利阿里群岛大学电子版毕业证{UIBLetterUIB文凭证书}文凭复刻
Taqyea
 
PDF
BRKACI-1001 - Your First 7 Days of ACI.pdf
fcesargonca
 
PPTX
PHIPA-Compliant Web Hosting in Toronto: What Healthcare Providers Must Know
steve198109
 
PPTX
Metaphysics_Presentation_With_Visuals.pptx
erikjohnsales1
 
PPTX
Orchestrating things in Angular application
Peter Abraham
 
PPTX
Presentation3gsgsgsgsdfgadgsfgfgsfgagsfgsfgzfdgsdgs.pptx
SUB03
 
DOCX
Custom vs. Off-the-Shelf Banking Software
KristenCarter35
 
PPTX
Networking_Essentials_version_3.0_-_Module_5.pptx
ryan622010
 
PPTX
法国巴黎第二大学本科毕业证{Paris 2学费发票Paris 2成绩单}办理方法
Taqyea
 
PDF
BRKSP-2551 - Introduction to Segment Routing.pdf
fcesargonca
 
PDF
FutureCon Seattle 2025 Presentation Slides - You Had One Job
Suzanne Aldrich
 
PPTX
Softuni - Psychology of entrepreneurship
Kalin Karakehayov
 
PDF
BRKACI-1003 ACI Brownfield Migration - Real World Experiences and Best Practi...
fcesargonca
 
PPTX
Lec15_Mutability Immutability-converted.pptx
khanjahanzaib1
 
PDF
BRKAPP-1102 - Proactive Network and Application Monitoring.pdf
fcesargonca
 
PPTX
L1A Season 1 ENGLISH made by A hegy fixed
toszolder91
 
PPTX
04 Output 1 Instruments & Tools (3).pptx
GEDYIONGebre
 
PDF
The Internet - By the numbers, presented at npNOG 11
APNIC
 
PDF
Digital burnout toolkit for youth workers and teachers
asociatiastart123
 
PDF
Enhancing Parental Roles in Protecting Children from Online Sexual Exploitati...
ICT Frame Magazine Pvt. Ltd.
 
西班牙巴利阿里群岛大学电子版毕业证{UIBLetterUIB文凭证书}文凭复刻
Taqyea
 
BRKACI-1001 - Your First 7 Days of ACI.pdf
fcesargonca
 
PHIPA-Compliant Web Hosting in Toronto: What Healthcare Providers Must Know
steve198109
 
Metaphysics_Presentation_With_Visuals.pptx
erikjohnsales1
 
Orchestrating things in Angular application
Peter Abraham
 
Presentation3gsgsgsgsdfgadgsfgfgsfgagsfgsfgzfdgsdgs.pptx
SUB03
 
Custom vs. Off-the-Shelf Banking Software
KristenCarter35
 
Networking_Essentials_version_3.0_-_Module_5.pptx
ryan622010
 
法国巴黎第二大学本科毕业证{Paris 2学费发票Paris 2成绩单}办理方法
Taqyea
 
BRKSP-2551 - Introduction to Segment Routing.pdf
fcesargonca
 
FutureCon Seattle 2025 Presentation Slides - You Had One Job
Suzanne Aldrich
 
Softuni - Psychology of entrepreneurship
Kalin Karakehayov
 
BRKACI-1003 ACI Brownfield Migration - Real World Experiences and Best Practi...
fcesargonca
 
Lec15_Mutability Immutability-converted.pptx
khanjahanzaib1
 
BRKAPP-1102 - Proactive Network and Application Monitoring.pdf
fcesargonca
 
L1A Season 1 ENGLISH made by A hegy fixed
toszolder91
 
04 Output 1 Instruments & Tools (3).pptx
GEDYIONGebre
 
The Internet - By the numbers, presented at npNOG 11
APNIC
 
Digital burnout toolkit for youth workers and teachers
asociatiastart123
 
Enhancing Parental Roles in Protecting Children from Online Sexual Exploitati...
ICT Frame Magazine Pvt. Ltd.
 

React Context API