react-context-hooks-example icon indicating copy to clipboard operation
react-context-hooks-example copied to clipboard

How to manage multi context?

Open thearabbit opened this issue 5 years ago • 3 comments
trafficstars

First thanks for your example. How to manage multi context:

  • Articles
  • Posts
  • Comments
  • Tasks
  • ...

Could example?

thearabbit avatar Jan 19 '20 12:01 thearabbit

I down't want to use Nested Provider, and could use each others

function App() {
  return (
    <ArticleProvider>
      < Posts />
            // Others Provider
             <Component />
      </Posts>
    </ArticleProvider>
  );
}

thearabbit avatar Jan 20 '20 21:01 thearabbit

Hi and sorry for the delay, you can share your code (repo) with me please, in order for me to check it, and I'll show you how to handle multiple contexts.

ibrahima92 avatar Jan 21 '20 12:01 ibrahima92

thanks for your reply. for example I have many context

- Articles
- Posts
- Comments
- Tasks

And then I will config its on App startup

function App() {
  return (
    <ArticleProvider>
      < PostsProvider />
          < CommentProvider />
                 ............
                  <App />
          < CommentProvider />
      </PostsProvider>
    </ArticleProvider>
  );
}

It is not good to config, if we have many contexts!!!!! After finding npm package, I found https://github.com/VeprUA/react-multi-provider to config multi contexts like this

  <MultiProvider
    providers={[
      <ContextProvider1/>,
      <ContextProvider2/>,
      <ContextProvider3/>,
      ...
  ]}>
    <SomeComponent/>
  </MultiProvider>

But I still problem on share context each others. For example We can't use ContextProvider1 in ContextProvider2, 3

Please help me...

thearabbit avatar Jan 22 '20 01:01 thearabbit