Add support for multiple Context values
See original GitHub issueThis is a request to support consuming multiple contexts from within a class component without creating artificial wrapper components. Looking at the new hooks proposal it seems that the underlying infrastructure is capable of propagating changes from multiple contexts to a single component but this does not seem to be exposing outside of hooks. I would love to see it available in class components using an approach not unlike the old context API where you declare a static contextTypes
field on the function which would expose multiple values to the component…
To differentiate from the old api the value in the contextTypes
object MUST be the context object.
// Theme context, default to light theme
const ThemeContext = React.createContext('light');
// Signed-in user context
const UserContext = React.createContext({name: 'Guest'});
Button.contextTypes = {
theme: ThemeContext,
user: UserContext
};
This should be able to be distinguished from the old context API where the props are typically PropTypes.*
references. i.e
Button.contextTypes = {
color: PropTypes.string
};
Issue Analytics
- State:
- Created 5 years ago
- Reactions:27
- Comments:7 (1 by maintainers)
We opted not to support this because of extra object allocations this would incur and extra static typing difficulties.
Honestly, why would you shoot yourself in the leg with classes when you can use hooks ? 😃 Yea sure it’s still alpha and not released officially, but I believe this is one of the reasons the team came up with hooks so you don’t need to do such craziness with classes.