Class 34
Resources:
- Understanding React Context API
- Using Context API in React (Hooks and Classes)
- The React Context hell
- Context / React Docs
Review, Research, and Discussion
- Why is the Context API useful?
- The Context API was created to solve a specific problem in React, sharing state down a component tree. Similar to the solution that Redux and React-Redux libraries solve, this solution prevents prop drilling. Prop drilling is when you have to continuously pass props down through a component tree in order for a component “way down” in the application to use that prop.
- Can a component outside of a provider get its context?
- The most common way to access
Contextfrom a class component is via thestatic contextType. - The traditional way to retrieve
Contextvalues was by wrapping the child component in theConsumer.
- The most common way to access
- What are some common use cases for using the Context API?
- Context is designed to share data that can be considered “global” for a tree of React components, such as: the current authenticated user, theme, or preferred language.
- Describe “Context Hell”
- React Context hell is the nasty code you get taking advantage of the React
Context API.
- React Context hell is the nasty code you get taking advantage of the React
Vocabulary Terms
- Global state
- data that we can access at any level of React App Tree
- Global context
- Context provides a way to pass data through the component tree without having to pass props down manually at every level.
- Provider
- Provider is the container for all React components. It may be used to define the theme, locale, and other application level settings, and can also be used to provide common properties to a group of components.
- Consumer
- A React component that subscribes to context changes. Using this component lets you subscribe to a context within a function component.