React useContext Hook – The Easy Way to Share State
React’s useContext
Hook makes it simple to share state between components without the need for “prop drilling.”
The Problem: Prop Drilling
When many components need access to the same state, we end up passing props down through components that don’t need them.
In this case, user
is passed through all components just to reach Component5
. That’s a lot of boilerplate!
The Solution
With useContext
, you can directly access shared data from any component—no matter how deeply nested.
1. Create the Context
2. Wrap Your App with the Provider
3. Access Context with useContext
Summary
✅ Avoid prop drilling ✅ Share state across many components ✅ Cleaner and more maintainable code