Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/StringEpsilon/mini-create-react-context
(A smaller) polyfill for the react context API
https://github.com/StringEpsilon/mini-create-react-context
context contexttypes polyfill ponyfill react typescript
Last synced: 2 months ago
JSON representation
(A smaller) polyfill for the react context API
- Host: GitHub
- URL: https://github.com/StringEpsilon/mini-create-react-context
- Owner: StringEpsilon
- License: mit
- Archived: true
- Fork: true (jamiebuilds/create-react-context)
- Created: 2019-04-05T18:04:29.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2021-11-22T04:02:21.000Z (about 3 years ago)
- Last Synced: 2024-10-28T12:11:50.436Z (3 months ago)
- Topics: context, contexttypes, polyfill, ponyfill, react, typescript
- Language: TypeScript
- Homepage:
- Size: 2.55 MB
- Stars: 34
- Watchers: 3
- Forks: 11
- Open Issues: 20
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-star-libs - StringEpsilon / mini-create-react-context
README
# mini-create-react-context
> (A smaller) Polyfill for the [React context API](https://github.com/reactjs/rfcs/pull/2)
## Install
```sh
npm install mini-create-react-context
```You'll need to also have `react` and `prop-types` installed.
## API
```js
const Context = createReactContext(defaultValue);
/*
{children}
...
{value => children}
*/
```## Example
```js
// @flow
import React, { type Node } from 'react';
import createReactContext, { type Context } from 'mini-create-react-context';type Theme = 'light' | 'dark';
// Pass a default theme to ensure type correctness
const ThemeContext: Context = 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)
{
this.setState(state => ({
theme: state.theme === 'light' ? 'dark' : 'light'
}));
}}
>
Toggle theme
{this.props.children}
);
}
}class Title extends React.Component<{ children: Node }> {
render() {
return (
// The Consumer uses a render prop API. Avoids conflicts in the
// props namespace.
{theme => (
{this.props.children}
)}
);
}
}
```## 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
```
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 `` can only receive a single child element. To fix the error just wrap everyting in a single `
`:```js
```
## Size difference to the original:
| | original | **mini**
|------------|----------|-----
|install size| [**50 kB**](https://packagephobia.now.sh/result?p=create-react-context) | [140 kB](https://packagephobia.now.sh/result?p=mini-create-react-context)
|minified | [3.3 kB](https://bundlephobia.com/result?p=create-react-context) | [**2.3kB**](https://bundlephobia.com/result?p=mini-create-react-context)
|minzip | 1.3 kB | **1.0kB**