React Context & Hooks Tutorial #2 - What is the Context API?
Skills:
React90%
Key Takeaways
Explains the React Context API for central state management in React applications
Full Transcript
okay then going so the first thing I'd like to talk about is the context API so the context API gives us a way to share state up and down a component tree quite easily now you can do that without the context API and know that you can do that with props but sometimes that can get a little bit messy especially as your component trees an application as a whole becomes bigger so the context API aims to solve this issue and it's going to do that by giving us a central place to store data or State and then share it between components without having to pass it down as props so it's going to clean things up for us a little and it's gonna make working with shared data much much easier now if you're familiar with redox this concept might seem a little familiar having this central place where data is stored and it can be accessed without the need to continually pass down props several components deep sometimes so the context API is now a different approach to this it's an alternative to redux if you like and when we use it alongside hooks it can behave very very similar to the way Redux behaves so imagine this scenario imagine we have an application with this component tree right here so we have app sitting at the root and page view and navbar then in the navbar we have a status bar then a book list component then book details add book quite simplistic and I'll but just bear with me now imagine we also have some state defined in the root component right here and that state is the UI theme so is this gonna be a dark theme or a light theme or it could be fine colors or something like that either way it's a bit of data that we maybe want to share with some of our components in this tree so say for example we want these four components book list details add book and status bar to use this data then in order for that to happen normally what we do is we pass that down as props right through every single component until it reaches every component that uses it these four at the bottom so this is fine we can't still do this but what we're doing here is passing props everywhere even through components that don't necessarily use the prop so use the state directly these are acting as carrier components for props if you like they're just carrying the props from one component down to another so again this is okay you can still do this perfectly fine but this approach can sometimes get a bit messy especially when there's a lot of components in the chain and the application gets bigger so the context API tries to solve this problem of having to pass props down at every level even through components which don't use them directly so what we could do now instead of this approach is to create a new context in a new file somewhere which is basically us making a place where a shared state could be initially defined and set up now when we create a context we then have to provide it to our component tree so that the components in that tree can actually access it and get the data now we do this via the context provider which is just a react tag and it wraps whichever components need access to it so now every component inside this tree right here could if it wanted to have access to the shared State or data but we don't have to use that data in each of these components and now no longer are we passing props down through the components we're just directly accessing the data from the context in the components which need it so this is the basic premise of the context API and it can make working with shared data much easier in a lot of cases so then if we have a quick look at the context page here on the react website we can see when it advises to use context in an application because it might not always be the best solution so it says right here that it's designed to share data that can be considered global for a tree of react components such as the current authenticated user a UI theme or preferred language or maybe some other kind of data which is considered global from that particular tree of components right so if you're ever in a situation where you're unsure as to whether you should use context or something else actually self this question is the data that I want to share global and is it going to be used in several of the child components if so then context probably is a good shouts if not then there might be other options which are better suited for example component composition which you can read more about on this page down here so just bear in mind that context is not a catch-all for every different situation it's just for global States that's shared between many different components in a tree okay so one more thing before we start coding in this course I am going to be using the reactive developer tools for Chrome it's just gonna help us out when we're inspecting States and stuff like that inside the application so if you want to download this go to the Chrome Web Store and just search for react developer tools and install it into Chrome and then you're going to be able to do that as well so anyway now we know a little bit more about the context API and why we use it let's go ahead in the next tutorial and actually create our first context and provider
Original Description
Hey gang, in this React Context tutorial I'll explain what the Context API actually is and why we use it for central state management.
----------------------------------------
🐱💻 🐱💻 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
More on: React
View skill →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