Context #
- Allows you to pass down data throughout a component tree without explicitly passing props
- Used for data that is considered global for a tree of React components (e.g. theme)
myContext = React.createContext(defaultValue)creates a context object that contains a default value (this can be an empty object, a string, etc.)- This returns an object that can wrap components with
<myContext.provider value={{ test: 1 }}>, and every component in the tree will be able to read the object withthis.contextoruseContext(myContext) - The workflow is to create a context outside of the tree, use it’s provider within the tree, import it in the other files, and use the
useContexthook - For class based components, setting the
contextTypestatic property on the component makes the context available withthis.context
- This returns an object that can wrap components with