create-react-15-context
Polyfill for the proposed React context API
jamiebuilds/create-react-context.
This is a fork ofIt can be installed by: npm install --save create-react-15-context
It adds support for older versions of React (15+) by using a polyfill for React 16.2's new Fragment
component.
Upstream readme follows:
Install
yarn add create-react-context
You'll need to also have react
and prop-types
installed.
API
const Context = ;// <Context.Provider value={providedValue}>{children}</Context.Provider>// ...// <Context.Consumer>{value => children}</Context.Consumer>
Example
// @flow;; type Theme = 'light' | 'dark';// Pass a default theme to ensure type correctnessconst ThemeContext: Context<Theme> = ; Component< children: Node theme: Theme > state = theme: 'light' ; { return // Pass the current context value to the Provider's `value` prop. // Changes are detected using strict comparison (Object.is) <ThemeContextProvider value=thisstatetheme> <button onClick= { this; } > Toggle theme </button> thispropschildren </ThemeContextProvider> ; } Component< children: Node > { return // The Consumer uses a render prop API. Avoids conflicts in the // props namespace. <ThemeContextConsumer> <h1 style= color: theme === 'light' ? '#000' : '#fff' > thispropschildren </h1> </ThemeContextConsumer> ; }