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.

How will react solve nested contexts?

See original GitHub issue
<context1.Provider value={value1}>
  <context2.Provider value={value2}>
    <context3.Provider value={value3}>
      <context4.Provider value={value4}>
        <context5.Provider value={value5}>

        </context5.Provider>
      </context4.Provider>
    </context3.Provider>
  </context2.Provider>
</context1.Provider>
<context1.Consumer>
  {value1 => <context2.Consumer>
    {value2 => <context3.Consumer>
      {value3 => <context4.Consumer>
        {value4 => <context5.Consumer>
          {value5 => (
            null
          )}
        </context5.Consumer>}
      </context4.Consumer>}
    </context3.Consumer>}
  </context2.Consumer>}
</context1.Consumer>

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:26
  • Comments:32 (11 by maintainers)

github_iconTop GitHub Comments

360reactions
milesjcommented, Jan 18, 2019

I’ll be honest. If you’re encountering this kind of implementation, then your architecture design seems poor and you probably shouldn’t be using React context.

91reactions
alesmenzelsocialbakerscommented, May 10, 2019

@0xorial you dont really need to have a component for that, after all <></> is just a function call React.createElement. So you could simplify it to a compose function like:

const compose = (contexts, children) =>
  contexts.reduce((acc, [Context, value]) => {
    return <Context.Provider value={value}>{acc}</Context.Provider>;
  }, children);

and use it as:

import Context1 from './context1';
import Context2 from './context2';
import Context3 from './context3';
...
import Context15 from './context15';

const MyComponent = (props) => {
  // const value1..15 = ... get the values from somewhere ;

  return compose(
    [
      [Context1, value1],
      [Context2, value2],
      [Context3, value3],
      ...
      [Context15, value15],
    ],
    <SomeSubComponent/>
  );
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Nested Context — The underrated aspect that's probably ...
When setting up your React application, you can give your layout sections different names, and use those names within consuming components.
Read more >
Context - React
Context is primarily used when some data needs to be accessible by many components at different nesting levels. Apply it sparingly because it...
Read more >
Is nesting React Context Provider and consuming those with ...
And consuming those nested providers with a Context dependencie like this : import React, { useContext } from "react"; import { UserContext } ......
Read more >
An Introduction To Context Propagation In JavaScript
“Context provides a way to pass data through the component tree without having to pass props down manually at every level.” — React...
Read more >
React Context API: updating Context from a nested ...
Context API is a way to store and modify different states and then be able to have access to those states in any...
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 Hashnode Post

No results found