Why
There are some instances in your application state is needed by multiple components. I will use context if this shared state requires a lot of prop drilling. In the past, Redux was a popular solution to avoid prop drilling. However, I don't believe Redux is needed anymore. React's context api works great for this.
Use Cases - Important!
You should use React context for global state. That being said, there aren't that many pieces of global state. Some good examples of global state are the current user, the current language setting, or a map of feature flags.
You don't need to use context only for global state. Context can be applied to a specific sub-tree of your application.
It's common to have multiple sub-tree specific contexts.
Top comments (0)