Context API Introduction
Learn how to share data between React components without passing props through every intermediate component.
1. What Is the Context API?
The Context API is a React feature that allows components to share information without manually passing props through every level of the component tree.
For example, an application may need to share the logged-in user's information, the selected theme, or the language preference with multiple components. Context can help make this information available to components that need it.
2. What Is Prop Drilling?
Prop drilling happens when data is passed from a parent component to a deeply nested child through intermediate components that may not need the data themselves.
Example: Without Context API
function App() {
const studentName = "Samadhan";
return <Parent studentName={studentName} />;
}
function Parent({ studentName }) {
return <Child studentName={studentName} />;
}
function Child({ studentName }) {
return <h2>Student Name: {studentName}</h2>;
}
export default App;
Here, Parent passes studentName
to Child, even though the Parent only forwards
the value.
studentName
Forwards prop
Uses data
3. How Does Context API Help?
Context lets a parent provide a value to components below it in the component tree. A component can read that value without receiving it as a prop from every intermediate component.
Shares studentName
Reads Context
The intermediate Parent no longer needs to forward the student name. The Child can read the shared value from Context when it is connected to the appropriate provider.
4. Main Parts of Context API
createContext
Creates a Context object that represents the value you want to share between components.
Context Provider
Makes a Context value available to components in its descendant tree. In modern React, you can use the provider syntax supported by your React version.
useContext
Reads the value of a Context from a component. The component receives the value from the closest matching provider above it, or the Context default value if no provider is found.
5. Simple Real-Life Example
Imagine CIIT Training Institute has a student portal. The student's name may be needed by the dashboard, profile section, and course section.
Instead of passing the student's name through every intermediate component, Context can make it available to the dashboard, profile, and courses components.
6. When Should You Use Context API?
- When several components need the same logged-in user information.
- When multiple components need the selected theme.
- When language preferences must be shared across a component tree.
- When passing props through many intermediate components becomes difficult to maintain.
7. Practice Tasks
Complete these tasks to check your understanding.
- Explain the meaning of the React Context API in your own words.
- Create an example where a parent passes a student name to a child using props.
- Draw a diagram showing prop drilling through three components.
- Explain how Context can reduce the need to forward props through intermediate components.
- Write the names and purposes of createContext, Provider, and useContext.
- Give two real-world examples where Context may be useful.
Summary
The React Context API allows components to share information without passing props through every intermediate component. It is useful for shared values such as user information, themes, and language preferences. The main concepts are createContext, a Context Provider, and useContext. In the next lessons, we will learn how to create Context and use it in React components.