# create-react-context

> Polyfill for the proposed React context API

Latest version **0.3.0** (published 2019-06-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install create-react-context
pnpm add create-react-context
yarn add create-react-context
bun add create-react-context
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.0 |
| Published | 2019-06-05 |
| First published | 2017-12-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 13.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 686 |
| Author | James Kyle |
| Maintainers | thejameskyle |
| Keywords | react, context, contextTypes, polyfill, ponyfill |

## Links

- npm: https://www.npmjs.com/package/create-react-context
- Repository: https://github.com/thejameskyle/create-react-context
- Homepage: https://github.com/thejameskyle/create-react-context#readme
- Issues: https://github.com/thejameskyle/create-react-context/issues
- npm.io page: https://npm.io/package/create-react-context

## Dependencies (2)

- [gud](https://npm.io/package/gud.md) ^1.0.0
- [warning](https://npm.io/package/warning.md) ^4.0.3

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 0.3.0 (latest) — 2019-06-05
- 0.2.3 — 2018-08-27
- 0.2.2 — 2018-04-13
- 0.2.1 — 2018-02-27
- 0.2.0 — 2018-02-26
- 0.1.6 — 2018-02-02
- 0.1.5 — 2018-02-01
- 0.1.4 — 2018-01-16
- 0.1.3 — 2018-01-07
- 0.1.2 — 2017-12-08
- 0.1.1 — 2017-12-08
- 0.1.0 — 2017-12-08

## README

# create-react-context

> Polyfill for the [proposed React context API](https://github.com/reactjs/rfcs/pull/2)

## Install

```sh
yarn add create-react-context
```

You'll need to also have `react` and `prop-types` installed.

## API

```js
const Context = createReactContext(defaultValue);
// <Context.Provider value={providedValue}>{children}</Context.Provider>
// ...
// <Context.Consumer>{value => children}</Context.Consumer>
```

## Example

```js
// @flow
import React, { type Node } from 'react';
import createReactContext, { type Context } from 'create-react-context';

type Theme = 'light' | 'dark';
// Pass a default theme to ensure type correctness
const ThemeContext: Context<Theme> = createReactContext('light');

class ThemeToggler extends React.Component<
  { children: Node },
  { theme: Theme }
> {
  state = { theme: 'light' };
  render() {
    return (
      // Pass the current context value to the Provider's `value` prop.
      // Changes are detected using strict comparison (Object.is)
      <ThemeContext.Provider value={this.state.theme}>
        <button
          onClick={() => {
            this.setState(state => ({
              theme: state.theme === 'light' ? 'dark' : 'light'
            }));
          }}
        >
          Toggle theme
        </button>
        {this.props.children}
      </ThemeContext.Provider>
    );
  }
}

class Title extends React.Component<{ children: Node }> {
  render() {
    return (
      // The Consumer uses a render prop API. Avoids conflicts in the
      // props namespace.
      <ThemeContext.Consumer>
        {theme => (
          <h1 style={{ color: theme === 'light' ? '#000' : '#fff' }}>
            {this.props.children}
          </h1>
        )}
      </ThemeContext.Consumer>
    );
  }
}
```

## Compatibility

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.

For example, you cannot pass children types aren't valid pre React 16:

```js
<Context.Provider>
  <div/>
  <div/>
</Context.Provider>
```

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>`:

```js
<Context.Provider>
  <div>
    <div/>
    <div/>
  </div>
</Context.Provider>
```

---
_Source: https://npm.io/package/create-react-context · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
