Good way to apply multiple Context with React Hook

Viewed 2776

Currently, I have CategoryContext to manage Category state, ModalContext to manage Modal state.

<Layout>
   <ModalContext.Provider>
   <Route>
       <CategoryContext.Provider />
           <CategoryPage />
   </Route>
   <ModalContext.Provider />
</Layout>

I'm just move from redux to react-hook to manage state. Then I'm not sure my solution is good or not? I'm happy to get more idea from social :)

1 Answers

You can have multiple context Providers that wrap components you wish to use context in. However in your case you aren't wrapping the component with provider but just rendering it in case of CategoryContext

<Layout>
   <ModalContext.Provider>
   <Route>
       <CategoryContext.Provider >
           <CategoryPage />
       </CategoryContext.Provider >
   </Route>
   <ModalContext.Provider />
</Layout>
Related