React Context & Hooks Tutorial #4 - Accessing Context (part 1)

Net Ninja · Beginner ·🌐 Frontend Engineering ·7y ago

Key Takeaways

Demonstrates how to access context data using the contextType property in React class components

Full Transcript

southern Ganga now we have our theme context file over here and we have our theme context provider which is wrapping these other two components right here so we want to consume that context from these two components now ie access the data that we pass in to this provider and we do that remember right here so how do we access this data from this context in these different components now well there's a couple of different ways we can do this inside a class component and the way that we're going to look at in this video is by using something called the context type so this way can be used in a class component but not in functional components and that's okay for now because we're using class components right here so let's go into navbar first of all and the way we use this is by saying static and it needs to be called context type like that and we set it equal to whatever context that we want to consume inside this component so we want to consume the theme context so press Enter it's going to automatically import that for me at the top so remember this is the theme context not the theme context provider the theme context itself and there is a difference this is the theme context we create and this is the provider right here which provides the context so we're saying that the context type of this component that we want to use is the theme context right here and what this does is now look up the component tree - the first time it finds a provider for this context and if it goes one level above this component is going to come to a PS and it's going to find that theme context provider right there and then it's going to have access to the data provided by this that we put into the value property right here so what it does is take all of this data that we pass in to the value property and it attaches it to a context property inside this component now so I could say for example inside the render method that I want to console dot log this to reference this component dot context so that should hopefully be equal now to this thing over here that we pass through into the value so if we go over here and look in the console we can see this data right here so this is what we log it out and it's exactly at the data that we pass through into that provided so that's good now we have access to all of this shared data inside this navbar component and that was dead simple all we have to do is say static context type is equal to theme context and import that up here and now we have access to all that data on this dot context so what I'm going to do now is just a bit of destructuring to get some different properties from this context because we have these three properties dark is light theme and lights and I want to store all of those in separate constants so I'll say Const and then destructuring so curly braces then we want the properties is light theme we also want light that was a property and dark so we're getting all of those properties now from this dot context okay so this is just D structuring all we're doing is getting these properties from this context object and we're storing them in variable names that are the same as these things so we have access to these three variables now all constants so the first thing I want to do is figure out if we're going to use the light theme or the dark theme inside this component now we can figure that out by evaluating this thing right here because that's the boolean remember it's true to begin with but it could be false so I want to figure out what this is if it's true then I'm going to say well okay the theme is the light theme that we're going to use so I'll use these colors if this is false now I'll say ok well we're using the dark theme and I'll use these colors so we use a ternary operator to decide this I'll say Const and we'll call this theme and that's going to represent what theme we're currently using and I'll set it equal to is light theme this is what we're going to evaluate question mark now if this is true then it's going to return the first value over here in which case we want it to use the light theme then we say a colon if this evaluates to false then it's going to return the second value on the right side of the colon and that is going to be the dark theme so now if this is true then theme will equal two lights if this is false then theme will equal to dark so now we have those different properties stored in this constant and then we can just output those inside this component right here so what do I want to do well I want to style the navbar first of all so let's do a style property and we're gonna do some bindings of curly braces and this takes an object and a different key value pairs so the first thing I want to style is the background and that is going to be the theme which is right here and we want the UI property on it dot UI because remember these have the UI property the BG property and the syntax property so we're just using these different properties now for our CSS in these different elements right here so that's the background color of the nav and then I also want to color the text so this will be the theme dot syntax so now we're colorizing this nav right here and pretty much that's all we need to do for this component so let me save this now and check it out in a browser come over here and now we can see it's colorized our app according to the theme that we currently have and remember if we're going to react tools over here and we open this up then go to the context provider we can see in here that is light is true so that means we're using the light theme but if I uncheck this it's going to change to use the dark theme okay so that's pretty cool right we can dynamically change this now and we'll add a button later on so that a user can do this without going into react tools because not every user is going to have that so that is the navbar door now let's do a similar thing inside this component right here so let's go now to book list and we want to do the same thing we say static and context type is equal to phim context so hit enter to automatically import that then we want to do a bit of D structure and again so inside the render method we'll say Const and we want to grab again is light theme the light theme and the dark theme and we get all of those from this dot context so again we need to say Const theme is equal to is light theme if this is true we want to return the first value which is going to be the light theme if not we'll return the dark theme so same as we did in the other component we're just working out what theme are currently aren't based on this boolean and we're storing that now inside theme so we can output it and the styles using that theme in the template so I'm going to do that over here I'm going to say style is equal to and then we want to ups we don't need those quotations just to lots of curly braces and inside we'll say color is theme dot syntax and the second property is going to be the background and that is going to be theme dot BG so I also want to do something similar for these Li tags right here so I'm just going to alt click all of these to do them all at once and I'll say style is equal to and then we want to use the background property and set it equal to theme dot UI okay so now we're colorizing this component as well so if we save that we should see that update in the browser cool so we're currently on the light theme if we now change this to the dark theme if I just expand this let me zoom this up you change this to false then it should change the theme over here and that's happening remember because of this ternary operator and every time this data changes in the context so every time if we go back to this every time the state changes then anything that's consumed in the context theme context and takes in that value that is going to update with the new value as well and because that updates over here in the book list and also in navbar then this is going to update right so when this updates from true to false this right here this ternary operator returns something different when it's true it returns the lighter theme and we output those colors and when it's false then it returns the dark into theme and we have put those colors all right so that's a nice little way now to take data from our context and then do something with it in our different components okay so that's one of consuming context in a components in the next video I'm also going to show you another way to do this

Original Description

Hey gang, in this React context tutorial we'll take a look at our to access our context data using the static property contextType. This is one of a few ways we can access context. ---------------------------------------- 🐱‍💻 🐱‍💻 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 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
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…
Prepare for a frontend developer interview with Capgemini by reviewing JavaScript fundamentals and practicing common interview questions
Medium · JavaScript
10 Frontend Developer Tools to Boost Productivity in 2026
Boost frontend productivity with 10 essential tools for modern web app development
Medium · Programming
10 Frontend Developer Tools to Boost Productivity in 2026
Boost frontend productivity with top 10 developer tools in 2026
Medium · JavaScript
The US Frontend Engineer Market in 2026: A Data-Driven Reality Check (and the Bias That Stops Us Seeing It)
US frontend engineer hiring demand peaked in 2022 and remains flat-depressed in 2026, contrary to common assumptions
Dev.to AI
Up next
How to Open KIT Files (CodeKit Project)
File Extension Geeks
Watch →