What is create-react-context?
The create-react-context package is a polyfill for the React context API for React versions below 16.3. It allows you to create a context object which can be used to pass data through the component tree without having to pass props down manually at every level.
What are create-react-context's main functionalities?
Creating Context
This feature allows you to create a new context object. You can set a default value that can be used when a component does not have a matching Provider above it in the tree.
{"const MyContext = createReactContext(defaultValue);"}
Provider Component
The Provider component is used to set the value of the context to be accessible by all components that are its descendants.
{"<MyContext.Provider value={/* some value */}>{/* some child components */}</MyContext.Provider>"}
Consumer Component
The Consumer component is used to read the current context value, provided by the nearest matching Provider above it in the tree.
{"<MyContext.Consumer>{value => /* render something based on the context value */}</MyContext.Consumer>"}
Other packages similar to create-react-context
react-redux
React Redux uses a similar pattern with a Provider and connect function to pass down state from a Redux store. It's more complex and tailored for state management with Redux.
mobx-react
MobX-react provides components like Provider and inject to connect React components to MobX stores. It's similar but is specifically designed to work with MobX state management.
react-broadcast
React Broadcast is another library that provides a similar API for creating a broadcast channel that allows you to pass data through the component tree. It's less commonly used since the introduction of the official context API in React.
create-react-context
Polyfill for the proposed React context API
Install
yarn add create-react-context
You'll need to also have react
and prop-types
installed.
API
const Context = createReactContext(defaultValue);
Example
import React, { type Node } from 'react';
import createReactContext, { type Context } from 'create-react-context';
type Theme = 'light' | 'dark';
const ThemeContext: Context<Theme> = createReactContext('light');
class ThemeToggler extends React.Component<
{ children: Node },
{ theme: Theme }
> {
state = { theme: 'light' };
render() {
return (
<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 (
<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:
<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>
:
<Context.Provider>
<div>
<div/>
<div/>
</div>
</Context.Provider>