React Context & Hooks Tutorial #7 - Creating Multiple Contexts
Key Takeaways
Explains how to create multiple contexts in React for different types of data
Full Transcript
okay then gank so we've seen how to create a single context to store some kind of data in this case about a theme and then use that data in different components which can consume this context right so we did that using this provider right here and passing in this value but what if we want some different kind of data for example if we had some authentication data for the currently logged in user now would we put it in the same context well no that's not a good idea because this is for the theme and it doesn't make sense to put some kind of authentication stuff inside this context what we can do is just create multiple different contexts so if we have some kind of data that is for one part of our application we could create a context for that if we have some other kind of data which is going to be global for another part of the application we could just create another context for that so let's go ahead and do that we'll create a new file called auth context is and inside here again we're going to import react and also component so let me do that snippet and we also need to import create context because that's the function we need to create the context then down here we'll say export Const and we'll call this off context and set it equal to create context so we're just going to be following the same pattern as we did do for this thing over here theme context so down here now let's do a class component so CC and then this is going to be called auth context provider we will have some state and that state is just going to hold maybe a simple boolean called is authenticated so I'm going to keep this really simple because I don't need to demonstrate how to use authentication in this series it's about using the context and this video is about using multiple contexts so I'm going to keep this as simple as possible just to show you how we can use two different contexts in our application so anyway let me set this to false to begin with and I'd also like to create a function now so that we can toggle this in the future if we want to so we'll call this toggle off and set it equal to an error function inside the arrow function we want to say this set state and the thing that we want to set is this property right here is authenticated so let's grab that and we want to change it to the reverse of whatever is now so exclamation mark then this dot state and then dot is authenticated so if it's false it's going to turn it to true and vice versa so that's this pretty much done over here the next thing we need to do is return some JSX and again this is going to be very similar to this thing over here where we have the provider then we have a value that we pass into that that's the data or functions we want to share and then we nest this dot props on which other components we want to provide this state or function to so let's go over here and do something similar this time we want the off context dot provider like so and in here we need allow you property and that is going to be equal to an object we use spread syntax to get this state and we also want to pass down a property called toggle auth which is going to be equal to this toggle off so this function right here then finally we just need to nest this dot props children like so so that is this context file pretty much done now the next thing we need to do is take this and we need to nest it inside our app dot J's file so that it surrounds the components that we want to use this context in so let's go ahead now to a pas to do this and we need to wrap these components with that new off context provider because these are the components that want to use that data or that function so we could nest the earth context provider inside the theme concepts provider like this or we could put it outside of it like this it really doesn't matter in this case they are both still wrapping these components so I'm going to put it inside so let me say auth context provider that's going to auto import it for me up there and now I need to take this closing tag and put it at the bottom over here and just scoot these in a little bit like that okay so now we have both of these context providers surrounding or wrapping these components so now these components should have access to both different contexts so if I save this at the minute and go to the browser over here then I'm going to open up the reactor tab and by the way Vista works so adding in this earth context provided inside themed content provider has not affected how this works this still works exactly the same now if we open this up we can see the first contact provider for the theme and then we have the second content provider inside for the authentication so if we see my soap we can see inside the value as well is authenticated true or false so now hopefully we can go ahead and start to consume that second context the auth context inside these components as well so the next step is to figure out how to consume multiple contexts in one component because for example inside the navbar over here we might want to consume not only the theme context but also the earth context and I'm going to show you how to do that how to consume multiple contexts in one component in the next video
Original Description
Hey gang, in this tutorial I'll show you how to create multiple different contexts for different, un-releated data.
----------------------------------------
🐱💻 🐱💻 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 Reads
📰
📰
📰
📰
Debugging React: From Taking a Deep Breath to Finding the Root Cause
Dev.to · surajrkhonde
Next.js vs Remix vs SvelteKit: Which Framework Should You Learn?
Dev.to · Etrit Neziri
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
🎓
Tutor Explanation
DeepCamp AI