1
|
# mini-create-react-context
|
2
|
|
3
|
<p align="center">
|
4
|
<a href="https://packagephobia.now.sh/result?p=mini-create-react-context">
|
5
|
<img alt="npm install size" src="https://packagephobia.now.sh/badge?p=mini-create-react-context">
|
6
|
</a>
|
7
|
<a href="https://bundlephobia.com/result?p=mini-create-react-context">
|
8
|
<img alt="npm bundle size" src="https://img.shields.io/bundlephobia/min/mini-create-react-context.svg?style=flat-square">
|
9
|
</a>
|
10
|
<a href="https://www.npmjs.com/package/mini-create-react-context">
|
11
|
<img alt="npm" src="https://img.shields.io/npm/v/mini-create-react-context.svg?style=flat-square">
|
12
|
</a>
|
13
|
</p>
|
14
|
|
15
|
> (A smaller) Polyfill for the [proposed React context API](https://github.com/reactjs/rfcs/pull/2)
|
16
|
|
17
|
## Install
|
18
|
|
19
|
```sh
|
20
|
npm install mini-create-react-context
|
21
|
```
|
22
|
|
23
|
You'll need to also have `react` and `prop-types` installed.
|
24
|
|
25
|
## API
|
26
|
|
27
|
```js
|
28
|
const Context = createReactContext(defaultValue);
|
29
|
/*
|
30
|
<Context.Provider value={providedValue}>
|
31
|
{children}
|
32
|
</Context.Provider>
|
33
|
|
34
|
...
|
35
|
|
36
|
<Context.Consumer>
|
37
|
{value => children}
|
38
|
</Context.Consumer>
|
39
|
*/
|
40
|
```
|
41
|
|
42
|
## Example
|
43
|
|
44
|
```js
|
45
|
// @flow
|
46
|
import React, { type Node } from 'react';
|
47
|
import createReactContext, { type Context } from 'mini-create-react-context';
|
48
|
|
49
|
type Theme = 'light' | 'dark';
|
50
|
// Pass a default theme to ensure type correctness
|
51
|
const ThemeContext: Context<Theme> = createReactContext('light');
|
52
|
|
53
|
class ThemeToggler extends React.Component<
|
54
|
{ children: Node },
|
55
|
{ theme: Theme }
|
56
|
> {
|
57
|
state = { theme: 'light' };
|
58
|
render() {
|
59
|
return (
|
60
|
// Pass the current context value to the Provider's `value` prop.
|
61
|
// Changes are detected using strict comparison (Object.is)
|
62
|
<ThemeContext.Provider value={this.state.theme}>
|
63
|
<button
|
64
|
onClick={() => {
|
65
|
this.setState(state => ({
|
66
|
theme: state.theme === 'light' ? 'dark' : 'light'
|
67
|
}));
|
68
|
}}
|
69
|
>
|
70
|
Toggle theme
|
71
|
</button>
|
72
|
{this.props.children}
|
73
|
</ThemeContext.Provider>
|
74
|
);
|
75
|
}
|
76
|
}
|
77
|
|
78
|
class Title extends React.Component<{ children: Node }> {
|
79
|
render() {
|
80
|
return (
|
81
|
// The Consumer uses a render prop API. Avoids conflicts in the
|
82
|
// props namespace.
|
83
|
<ThemeContext.Consumer>
|
84
|
{theme => (
|
85
|
<h1 style={{ color: theme === 'light' ? '#000' : '#fff' }}>
|
86
|
{this.props.children}
|
87
|
</h1>
|
88
|
)}
|
89
|
</ThemeContext.Consumer>
|
90
|
);
|
91
|
}
|
92
|
}
|
93
|
```
|
94
|
|
95
|
## Compatibility
|
96
|
|
97
|
This package only "ponyfills" the `React.createContext` API, not other unrelated React 16+ APIs. If you are using a version of React <16, keep in mind that you can only use features available in that version.
|
98
|
|
99
|
For example, you cannot pass children types aren't valid pre React 16:
|
100
|
|
101
|
```js
|
102
|
<Context.Provider>
|
103
|
<div/>
|
104
|
<div/>
|
105
|
</Context.Provider>
|
106
|
```
|
107
|
|
108
|
It will throw `A valid React element (or null) must be returned. You may have returned undefined, an array or some other invalid object.` because `<Context.Provider>` can only receive a single child element. To fix the error just wrap everyting in a single `<div>`:
|
109
|
|
110
|
```js
|
111
|
<Context.Provider>
|
112
|
<div>
|
113
|
<div/>
|
114
|
<div/>
|
115
|
</div>
|
116
|
</Context.Provider>
|
117
|
```
|