react-combine-contexts
Use multiple React Contexts without pain.
Installation
This package is compatible with react 16.3.0 and higher.
With npm:
npm install react-combine-contexts
Or with yarn:
yarn add react-combine-contexts
Usage
// src/index.js;; ; ;; // This will return an object that contains a Provider and a Consumerconst CombinedContext = ; const App = <Fragment> /* Use the Context Provider like you would use it with any other Context */ <CombinedContextProvider> <Fragment> /* Use multiple contexts with a single Consumer */ <CombinedContextConsumer> <div style= color: themecolor fontFamily: themefontFamily > <span>Selected locale is localeselectedLocale</span> <div> <button onClick= locale>en</button> <button onClick= locale>fr</button> </div> </div> </CombinedContextConsumer> /* It is still possible to use a scoped Consumer */ <ThemeContextConsumer> <div>The main color is themecolor</div> </ThemeContextConsumer> </Fragment> </CombinedContextProvider> </Fragment>; ReactDOM;
combineContexts
This function takes an object as a parameter and returns another object which contains a Provider and a Consumer.
For each key of the provided object you must provide a Context. The keys you indicated will be the ones used by the Consumer in the object passed as the parameter of its child function.
Inspiration
This package is inspired by react-adopt.
License
MIT