react-context-connector
React HOC to the new Context API to keep the use as simple as React-Redux connect HOC.
Usage
connectContext(ContextProvider, mapContextToProps)
Generates a HOC to wrap a component with context values in props.
Parameters
ContextProvider: Object
, an Object with Consumer
attribute, like the Context object or a Custom Provider
mapContextToProps: function
, a functiona that receives the context from Context.Consumer
and returns a object to be used as props for the wrapped component.
Returns: Component
, A HOC which pass context state into props to child component.
Example
Create your Context Provider
const CounterContext = React; state = count: 0 { ... } { ... } { return <CounterContextProvider value=thisstate> thispropschildren </CounterContextProvider> ; } CounterProviderConsumer = CounterContextConsumer;
Create your component and connect it to the Context
;; const CounterDisplay = <div> Counter: value </div>; CounterDisplaypropTypes = value: PropTypesnumber; // map context state and functions to component propsconst mapContextToProps = value: contextcount; const CounterDisplayWithContext = CounterDisplay; ;
Create your App and wrap with the Context Provider
const App = <CounterProvider> <div> <CounterDisplay /> <CounterButtons /> </div> </CounterProvider>; ;
Check the example folder for more details.