React Context & Hooks Tutorial #8 - Consuming Multiple Contexts
Key Takeaways
Shows how to consume multiple contexts in a single component in React
Full Transcript
or rather than gangs so in this video I'd like to show you how to consume two different contexts inside a single component so I've shown you so far two separate ways to consume a context I've shown you this way where we say static context type is equal to whatever context that we want to use or we say consumer like this and then get the context object in this function so if we want to use two separate contexts inside a component how do we go about that because now we have two contexts we have the theme one and the earth one so if I wanted to do inside here for example I couldn't just say static context type is equal to earth context that's not going to work because then we have two static properties with the same name and it's just not going to work that so we have two options here what we could do is we could use one context this way using the context type property and then the other one is going to use the consumer tag down here or we can just use to consumer tags so we can nest one inside the other and that's the way I'm going to show you in this video so what I'm going to do is surround all of this right here with the auth context consumer so to do that I'm just going to go up here and say auth context and press Enter that's going to automatically import it for me then we want to use dot consumer like so and then I'm going to place this right at the end down here now remember this expects a function to go inside of it and that function is going to return some JSX so this function is also going to take in a context object I'm going to call this off context I'm not calling it context because we already have that down here and in fact I'm going to call this one theme context so we know which each one is so anyway inside this top function all we're going to do is return some JSX so I don't need to open up a function I'm just going to return the JSX inside these parentheses so the J a sex we want to return is all of this stuff right here so let me paste that in like so and just scoot this back up here so now we have access to two different contexts inside this component the auth one and the theme one now I'm just going to change this to theme context because we renamed that variable and now also I'm going to do a similar thing to destructure some information from earth context because remember we had this stuff right here is authenticated and also toggle auth we passed both of those into the value property so let me now do a bit of destructuring next to these ones to grab those I'll say Const and we want is authenticated and we also want toggle off I think that's why it was called let me just double check yet toggle off and toggle off so now we have access to both of those things are set it equal to off context to grab the properties from there and now down here we could do something with these things so for now underneath the h1 I'm going to do a div and inside I'm going to output whether the user is logged in or logged out now to do that I need to evaluate this which is a billion it's how the true or false so I use a ternary operator and to do that we'll say is authenticated which is going to be evaluated to true or false if it's true we'll say logged in and if it's false we'll say logged out okay so now if I save this and come over here then we should see we are logged out but if I inspect and then go to react what I'm going to do is come down here to the consumer and go up and in fact we need to go to the provider not the consumer and change the value to true so now we can see unlocked in and as we toggle this it's logging me in and out well it's not actually locking me in or out but it's changing what we display right here and everything else still works as well so the theme context is still working in this navbar as well so that's nice we've now consumed two different contexts in one single ponen now I want to do one more thing and that is to use this toggle or function when we click on this div so whatever we click over here on this thing I'd like it to toggle the authentication status so I'll say on click oops on click and set that equal to a function which is called toggle off and then I just need to save this and go over here and let's see if we click it now you can see it toggles the authentication status so there we go my friends that's how you consume two contexts in one single component now all of this was fine but it actually becomes much easier to do all of this when we use the context API in combination with react hooks and it means that we won't have to do all of this consumer stuff right here so for the next few videos I'm going to be talking about react hooks and how they work then eventually we'll be coming around full circle to use hooks with the context API which is going to make working with all of this kind of stuff so much easier
Original Description
Hey gang, in this React context tutorial I'll show you how to consume multiple contexts in one single component.
----------------------------------------
🐱💻 🐱💻 Course Links:
+ Course files - https://github.com/iamshaunjp/react-context-hooks
+ Complete React Course - https://www.youtube.com/playlist?list=PL4cUxeGkcC9ij8CfkAY2RAGb-tmkNwQHG
+ React Context Docs - https://reactjs.org/docs/context.html
+ Modern JavaScript Tutorial - https://www.udemy.com/modern-javascript-from-novice-to-ninja/?couponCode=NINJAYT
🐱💻 🐱💻 Other Related Courses:
+ Firebase Firestore Playlist - https://www.youtube.com/watch?v=4d-gIPGzmK4&list=PL4cUxeGkcC9itfjle0ji1xOZ2cjRGY_WB
Firebase Authentication Tutorial - https://www.youtube.com/watch?v=aN1LnNq4z54&list=PL4cUxeGkcC9jUPIes_B8vRjn1_GaplOPQ
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Net Ninja · Net Ninja · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
GraphQL Tutorial #5 - Express App Setup
Net Ninja
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
GraphQL Tutorial #8 - Root Query
Net Ninja
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
GraphQL Tutorial #12 - Author Type
Net Ninja
GraphQL Tutorial #13 - Type Relations
Net Ninja
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
GraphQL Tutorial #18 - Mutations
Net Ninja
GraphQL Tutorial #19 - More on Mutations
Net Ninja
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
GraphQL Tutorial #23 - Create React App
Net Ninja
GraphQL Tutorial #24 - Book List Component
Net Ninja
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
GraphQL Tutorial #28 - Add Book Component
Net Ninja
GraphQL Tutorial #29 - External Query File
Net Ninja
GraphQL Tutorial #30 - Updating Component State
Net Ninja
GraphQL Tutorial #31 - Composing Queries
Net Ninja
GraphQL Tutorial #32 - query variables
Net Ninja
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
GraphQL Tutorial #34 - Book Details Component
Net Ninja
GraphQL Tutorial #36 - Styling the App
Net Ninja
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
Updated Vue & Firebase Course (Udemy)
Net Ninja
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja
Related AI Lessons
⚡
⚡
⚡
⚡
Had my Frontend Developer interview with Capgemini (Application Developer) today, and I wanted to…
Medium · JavaScript
10 Frontend Developer Tools to Boost Productivity in 2026
Medium · Programming
10 Frontend Developer Tools to Boost Productivity in 2026
Medium · JavaScript
The US Frontend Engineer Market in 2026: A Data-Driven Reality Check (and the Bias That Stops Us Seeing It)
Dev.to AI
🎓
Tutor Explanation
DeepCamp AI