create-react-context

WebJar for create-react-context

License

License

MIT
Categories

Categories

React User Interface Web Frameworks
GroupId

GroupId

org.webjars.npm
ArtifactId

ArtifactId

create-react-context
Last Version

Last Version

0.3.0
Release Date

Release Date

Type

Type

jar
Description

Description

create-react-context
WebJar for create-react-context
Project URL

Project URL

http://webjars.org
Source Code Management

Source Code Management

https://github.com/jamiebuilds/create-react-context

Download create-react-context

How to add to project

<!-- https://jarcasting.com/artifacts/org.webjars.npm/create-react-context/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>create-react-context</artifactId>
    <version>0.3.0</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/create-react-context/
implementation 'org.webjars.npm:create-react-context:0.3.0'
// https://jarcasting.com/artifacts/org.webjars.npm/create-react-context/
implementation ("org.webjars.npm:create-react-context:0.3.0")
'org.webjars.npm:create-react-context:jar:0.3.0'
<dependency org="org.webjars.npm" name="create-react-context" rev="0.3.0">
  <artifact name="create-react-context" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='create-react-context', version='0.3.0')
)
libraryDependencies += "org.webjars.npm" % "create-react-context" % "0.3.0"
[org.webjars.npm/create-react-context "0.3.0"]

Dependencies

compile (2)

Group / Artifact Type Version
org.webjars.npm : gud jar [1.0.0,2)
org.webjars.npm : warning jar [4.0.3,5)

Project Modules

There are no modules declared in this project.

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);
// <Context.Provider value={providedValue}>{children}</Context.Provider>
// ...
// <Context.Consumer>{value => children}</Context.Consumer>

Example

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

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

Versions

Version
0.3.0
0.2.3
0.2.2
0.1.6