useContext
useContext
The useContext
hook helps components share data with each other.
It is very common to have:
a nested tree of components
a need for components to share data across branches of the tree
In such scenarios we would lift state up to a common parent and pass the data down as props. With a deeply nested component tree however, this results in having to pass the prop through multiple layers of components even if those layers don't need that prop at all. The useContext
hook gives us an alternative to this "prop drilling". It creates a direct portal between the component that provides the state (context) and the components that need to consume that state.
References
Last updated
Was this helpful?