question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add support for multiple Context values

See original GitHub issue

This 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:closed
  • Created 5 years ago
  • Reactions:27
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

47reactions
gaearoncommented, Nov 1, 2018

We opted not to support this because of extra object allocations this would incur and extra static typing difficulties.

10reactions
danielkczcommented, Jul 9, 2019

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use React Hooks Context with multiple values for ...
I've added a CodeSandbox to my answer that should help make this a lot more concrete and allow you to experiment with different...
Read more >
React Tutorial 50 - How to handle multiple context within React
Content:- In this video, I have showed how can we handle multiple context. Now unfortunately, we cannot implement multiple context in class ...
Read more >
19. Multiple Context with Context Consumer ... - YouTube
Hi FriendsIn this video, we will see how to access the multiple context providers using the context API consumer. Replace the context API ......
Read more >
CLI Book 1: Cisco ASA Series General Operations CLI ...
The system administrator adds and manages contexts by configuring each context configuration location, allocated interfaces, ...
Read more >
Context - React
Context provides a way to pass data through the component tree without having to pass props down manually at every level. In a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found