React Legacy Context Polyfill
The `create-react-context` package provides a polyfill, or more accurately, a ponyfill, for the modern React Context API (`React.createContext`) introduced in React 16.3.0. It allows applications running on older versions of React (specifically React 0.14.0, 15.0.0, and pre-16.3.0 versions of React 16) to utilize the `Provider` and `Consumer` pattern for prop drilling and state management, mimicking the official API's behavior. The current stable version is 0.3.0, and given its nature as a polyfill for a now-native feature, it is unlikely to receive significant new feature development; its release cadence is effectively dormant. Its primary differentiation was enabling forward-compatible context usage before React 16.3 became widespread, bridging the gap for applications migrating or needing modern context features in legacy environments, while explicitly *not* polyfilling other React 16+ APIs.
Common errors
-
A valid React element (or null) must be returned. You may have returned undefined, an array or some other invalid object.
cause Providing multiple direct children to the `<Context.Provider>` component when using an older React version (prior to React 16.0), which expects only a single child.fixEnsure that `<Context.Provider>` always receives a single child element. If you need to render multiple components, wrap them in a single `div` or similar parent element: `<Context.Provider><div><Child1/><Child2/></div></Context.Provider>`.
Warnings
- breaking This package is a polyfill for the `React.createContext` API introduced in React 16.3.0. For React versions 16.3 and above, you should use `React.createContext` directly from the `react` package, as this polyfill is no longer necessary and could cause compatibility issues or unnecessary bundle size.
- gotcha When using this polyfill with React versions prior to 16.0, the `<Context.Provider>` component can only accept a single child element. Providing multiple direct children will result in a runtime error.
Install
-
npm install create-react-context -
yarn add create-react-context -
pnpm add create-react-context
Imports
- createReactContext
import { createReactContext } from 'create-react-context';import createReactContext from 'create-react-context';
- Context (Type)
import { type Context } from 'create-react-context'; - createReactContext (CommonJS)
const { createReactContext } = require('create-react-context');const createReactContext = require('create-react-context');
Quickstart
import React, { useState, type ReactNode } from 'react';
import createReactContext, { type Context } from 'create-react-context';
type Theme = 'light' | 'dark';
const ThemeContext: Context<Theme> = createReactContext('light');
function ThemeToggler({ children }: { children: ReactNode }) {
const [theme, setTheme] = useState<Theme>('light');
const toggleTheme = () => {
setTheme(prevTheme => (prevTheme === 'light' ? 'dark' : 'light'));
};
return (
<ThemeContext.Provider value={theme}>
<button onClick={toggleTheme}>
Toggle theme (current: {theme})
</button>
{children}
</ThemeContext.Provider>
);
}
function Title({ children }: { children: ReactNode }) {
return (
<ThemeContext.Consumer>
{theme => (
<h1 style={{ color: theme === 'light' ? '#000' : '#fff' }}>
{children}
</h1>
)}
</ThemeContext.Consumer>
);
}
// Example usage in an App component
function App() {
return (
<ThemeToggler>
<Title>Hello Context!</Title>
</ThemeToggler>
);
}
export default App;