npm install react-contextual
- consume and create context with ease, every kind of context, no matter which or whose or how many providers
- a cooked down redux-like store pattern with setState semantics and central actions
Click this link for a detailed explanation.
If you just need a light-weight no-frills store 🎰
import Provider Subscribe from 'react-contextual'const store =count: 0count: statecount + 1count: statecount - 1const App =<Provider ><Subscribe><div><h1>propscount</h1><button =>Up</button><button =>Down</button></div></Subscribe></Provider>
Higher Order Component
import Provider subscribe from 'react-contextual'const View =<div><h1>propscount</h1><button =>Up</button><button =>Down</button></div>const App =<Provider ><View /></Provider>
Default store vs External store
If you declare your store like above it becomes the default internal context, and is available by default to all subscribers. There is no need to explicitely refer to it when you subscribe to it.
When you need your store to be "external" so that you can refer to it and/or change its props from anywhere, you can declare it via createStore. This also comes in handy when you need multiple stores.
There are a few key differences:
- the store must be passed to its provider with the
- it must be referred to either as first argument in
import Provider createStore subscribe from 'react-contextual'const externalStore =const App =<Provider =><Subscribe => <div>propstext</div></Subscribe></Provider>
If you do not supply any functions in the object passed to createStore, a
setState function would be added automatically for you. This applies to both
createStore and the
const store =const Test =<button =>propscount</button>
subscribe and Subscribe (the component) work with any React context, even polyfills. They pick providers and select state. Extend wrapped components from
React.PureComponent and they will only render when picked state has changed.
// Subscribes to all contents of the provider// Picking a variable from the store, the component will only render when it changes ...// Picking a variable from the store using the components own props// Making store context available under the 'store' prop// Selecting several providers// Selecting several providers using the components own props// Making two providers available under the props 'theme' and 'store'
If you like to provide context 🚀
Contextual isn't limited to reading context and store patterns, it also helps you to create and share providers.
- moduleContext creates a global provider and injects it into a component
- namedContext creates a unique provider bound to a components lifecycle
- transformContext transforms existing providers (like a declarative middleware)
- helper functions allow you to control a context by yourself
Custom providers & transforms
import subscribe moduleContext transformContext from 'react-contextual'const Theme = context color children<context.Provider = = />const Invert = context color children<context.Provider = = />const Write = color text<span =>text</span>const App =<Theme ="red"><Write ="hello" /><Invert><Write ="world" /></Invert></Theme>
@PureComponent// ...@PureComponent// ...@PureComponent// ...