React Context & Global Data
In React, data flows top-down via props. But what happens when many components need the same data? We use the Context API.
The Problem: Prop Drilling
When you have to pass a prop through intermediate components that don't need the prop themselves, just to get it to a deeply nested child, it is called Prop Drilling. It makes code harder to maintain and components less reusable.
The Solution: Context
Context provides a way to share values like user data, themes, or language preferences across the entire component tree without having to explicitly pass a prop through every level. First, you initialize it with createContext().
Provider and Consumer
The <Provider value={data}> component wraps the part of your app that needs the data. Any component inside that Provider can use the useContext() hook to read the current value.
View Full Transcript+
This section contains the full detailed transcript of the video lessons. It covers why prop drilling is bad for scalability, how to initialize Context using React.createContext, setting up the Provider at a high level in the DOM tree, and finally consuming the context using the useContext hook in functional components.
