React
/Intermediate
Context vs Props
Definition
Props represent explicit, direct, one-to-one communication between components. Context represents implicit, broadcast, one-to-many communication across the component tree.
Explain Like I'm New
Props are like sending a text message directly to your friend. It is private, explicit, and easy to trace. Context is like making an announcement over a school PA system. Everyone in the building can hear it immediately without you having to find them, but if used too much, the school becomes chaotic and noisy.
Real World Example
Use Props for: The `label` on a specific button, the `imageUrl` on a profile picture, or the `items` in a shopping cart list. Use Context for: The active Theme (Dark/Light), the currently logged-in User Session, or the active Locale/Language (en-US).
Common Use Cases
- •Props: High component reusability, strict data flow, isolated logic.
- •Context: Global app state, avoiding Prop Drilling, ambient data.
Terminal Output
bash / terminal
// Conceptual comparison, no execution needed.
console.log("Props are Explicit. Context is Implicit. Prefer Props until the pain of drilling outweighs the loss of component reusability.");
Interview Questions
basic
- When should you use Props instead of Context?
- When should you use Context instead of Props?
intermediate
- Why does using Context make a component harder to reuse?
- How does a Context Provider update its consumers?
advanced
- What is the performance penalty of Context compared to Props?
- How can you optimize Context to prevent unnecessary renders?
trick
- Can a component receive data from both Props and Context at the same time?