Skip to content
(A smaller) polyfill for the react context API
TypeScript JavaScript
Branch: master
Clone or download
Pull request Compare This branch is 56 commits ahead, 2 commits behind jamiebuilds:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
src Chore: Upgrade dev dependencies to fix CJS build. Apr 13, 2019
types Fork: Apr 5, 2019
.editorconfig Added .editorconfig Jun 25, 2019
.gitignore Fork: Apr 5, 2019
LICENSE Add my name to LICENSE. Apr 5, 2019 Update Jun 6, 2019
babel.config.js Use typescript plugin for build. Apr 6, 2019
package.json Chore: Bump devDependencies / fix npm audit warnings Jul 22, 2019
rollup.config.js Chore: Upgrade dev dependencies to fix CJS build. Apr 13, 2019


npm install size npm bundle size npm

(A smaller) Polyfill for the React context API


npm install mini-create-react-context

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


const Context = createReactContext(defaultValue);
	<Context.Provider value={providedValue}>


		{value => children}


// @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<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 (
      <ThemeContext.Provider value={this.state.theme}>
          onClick={() => {
            this.setState(state => ({
              theme: state.theme === 'light' ? 'dark' : 'light'
          Toggle theme

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


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:


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


Size difference to the original:

original mini
install size 50 kB 140 kB
minified 3.3 kB 2.3kB
minzip 1.3 kB 1.0kB
You can’t perform that action at this time.