Context API vs Props
Understand the differences between Props and Context API and learn when to use each approach in React applications.
1. What Are Props?
Props are inputs passed from a parent component to a child component. They allow components to receive data and functions from their parent.
Props are useful when a parent needs to pass specific information to a child, such as a student name, course title, product price, or button callback.
function Student(props) {
return (
<div>
<h2>Student Name: {props.name}</h2>
<p>Course: {props.course}</p>
</div>
);
}
export default Student;
import Student from './Student';
function App() {
return (
<Student
name="Samadhan"
course="React Development"
/>
);
}
export default App;
Expected Output
Student Name: Samadhan
Course: React Development
2. What Is Context API?
Context API allows a component to read a value supplied by a matching Provider above it in the component tree. It reduces the need to pass the same value through intermediate components that do not need to use that value.
Context is useful for information shared by several components, such as the current theme, language preference, or logged-in user information.
import { createContext } from 'react';
const ThemeContext = createContext('light');
export default ThemeContext;
import ThemeContext from './ThemeContext';
import ThemeDisplay from './ThemeDisplay';
function App() {
return (
<ThemeContext.Provider value="dark">
<ThemeDisplay />
</ThemeContext.Provider>
);
}
export default App;
import { useContext } from 'react';
import ThemeContext from './ThemeContext';
function ThemeDisplay() {
const theme = useContext(ThemeContext);
return <h2>Current Theme: {theme}</h2>;
}
export default ThemeDisplay;
Expected Output
Current Theme: dark
3. Understanding Prop Drilling
Prop drilling happens when data must be passed through several intermediate components before reaching the component that actually needs it.
function App() {
const studentName = 'Samadhan';
return <Parent studentName={studentName} />;
}
function Parent({ studentName }) {
return <Child studentName={studentName} />;
}
function Child({ studentName }) {
return <h2>Student: {studentName}</h2>;
}
export default App;
Parent forwards studentName without using it for its own purpose. For a small component tree, props are simple and perfectly reasonable. If many intermediate levels need to forward the same data, Context may reduce that extra work.
4. Passing Data Through Props
Consider a student portal where the App component passes a student's name through a Parent component to a Profile component.
function App() {
return (
<Parent studentName="Samiksha" />
);
}
function Parent({ studentName }) {
return (
<Profile studentName={studentName} />
);
}
function Profile({ studentName }) {
return (
<h2>Welcome, {studentName}!</h2>
);
}
export default App;
Expected Output
Welcome, Samiksha!
Every intermediate component must pass the value along until it reaches the Profile component.
5. Sharing Data Through Context
Now we can make the student name available through Context, allowing Profile to read it directly.
import { createContext } from 'react';
const StudentContext = createContext('Guest Student');
export default StudentContext;
import StudentContext from './StudentContext';
import Parent from './Parent';
function App() {
return (
<StudentContext.Provider value="Samiksha">
<Parent />
</StudentContext.Provider>
);
}
export default App;
import Profile from './Profile';
function Parent() {
return <Profile />;
}
export default Parent;
import { useContext } from 'react';
import StudentContext from './StudentContext';
function Profile() {
const studentName = useContext(StudentContext);
return <h2>Welcome, {studentName}!</h2>;
}
export default Profile;
Expected Output
Welcome, Samiksha!
6. Context API vs Props: Comparison
| Feature | Props | Context API |
|---|---|---|
| Purpose | Pass data from parent to child. | Share values with descendant components. |
| Data flow | Explicitly passed through component props. | Read through a matching Provider and useContext. |
| Intermediate components | May need to forward props. | Can skip forwarding values they do not use. |
| Best suited for | Specific parent-to-child inputs. | Values shared across several descendants. |
| Setup | Pass props directly. | Create Context and provide its value. |
| Updates | Parent can pass updated props. | Provider can supply an updated value, often using state. |
7. When Should You Use Props?
- When passing a value to a direct child component.
- When a component needs a specific title, label, or item.
- When a parent needs to pass an event handler to a child.
- When explicit data flow makes the component easier to understand.
Example
function Course({ title }) {
return <h2>Course: {title}</h2>;
}
export default Course;
A Course component receiving its title as a prop is a straightforward use of props.
8. When Should You Use Context API?
- When many descendant components need the same theme.
- When several components need the current user's information.
- When multiple components need a shared language preference.
- When forwarding the same value through many intermediate components becomes difficult to maintain.
9. Can Props and Context Be Used Together?
Yes. Context can provide shared application information, while props can pass component-specific values.
import { useContext } from 'react';
import ThemeContext from './ThemeContext';
function CourseCard({ title }) {
const theme = useContext(ThemeContext);
return (
<div>
<h2>{title}</h2>
<p>Current Theme: {theme}</p>
</div>
);
}
export default CourseCard;
In this example, title comes from props and
theme comes from Context.
10. Common Mistakes
Mistake 1: Using Context for Every Value
Context is useful for shared information, but ordinary props are often simpler for direct parent-to-child communication.
Mistake 2: Forgetting the Provider
A component without a matching Provider above it receives the Context default value.
Mistake 3: Mutating Props
Props should be treated as read-only inputs. The parent should provide updated values rather than the child modifying its props.
Mistake 4: Assuming Context Replaces Props Completely
Context is not intended to eliminate props. Props remain useful for explicit component inputs and reusable components.
11. Practice Tasks
Complete these tasks to strengthen your understanding.
- Create a Student component that receives name and course through props.
- Pass data through three components and identify prop drilling.
- Create a StudentContext and display the student name without forwarding it through props.
- Create a ThemeContext and read its value inside a deeply nested component.
- Build a CourseCard component that receives its title through props and reads the theme from Context.
- Explain when props are more suitable than Context.
- Write five differences between Props and Context API.
Summary
Props pass explicit data and functions from a parent to its children, while Context makes shared values available to descendant components through a Provider. Props are suitable for direct component inputs, whereas Context can reduce prop drilling when many components need the same information. Both approaches can be used together to create clear, maintainable React applications.