0.3.0 • Published 5 years ago

create-react-context v0.3.0

Weekly downloads
4,106,874
License
MIT
Repository
github
Last release
5 years ago

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>
@reach/router@applicaster/zapp-react-native-utilstest-g-pc@invoice-simple/react-navigation@tomzio/brickui@ashversache/toaster@mallpay/routerblindajeybbtestui1jw-react-naver-maps@paraponera/antdreact-popper-mjktuireact-navigation-fixed-measureinwindowteasim-animationteasim-motion@react-navigation/react-navigation-experiment-core@react-navigation/react-navigation-experimental@infinitebrahmanuniverse/nolb-create-rnew-test-timeline-jomaycur-antdluck-design-test@everything-registry/sub-chunk-1408snbc-paas-uicpaas-uioui-antdpapo-componentspantuiosdoc-uinois-react-toastmki-react-routermobx-formstateparvus-uipaulshestakov-react-calendar-timelinenativesystemqd-clear-compontentpatched-react-i18nextpoc-react-calendar-timeline@arathunku/formikbirtly-calendar-timeline@dock365/react-form@cantonjs/react-scroll-view@canvas-panel/core@copart/ops-tool-kit@copart/reference-data@cn3lfs/react-calendar-timeline@clinia/uikit@cobuildlab/react-calendar-timeline@dragonraider5/react-intlbx-simple-mapcalendar-timeline-only-horizontalcalendar-of-akram@davidbabel/formik@databyss-org/reach-router@cyberinsight/cps-ui@cyannuro/react-calendar-timeline@cyber-insight/cps-ui@dauntless/react-calendar-timeline@vitalsource/vstui@vitali_shcherbina/styled-lib@tuomokar/formik@vieiraricardo/react-localize-redux@beisen-phoenix/react-table@beisen-phoenix/table@beisen-phoenix/table-v2@beisen-phoenix/tabszenform@any-ui/componentszh-react-navigation@atto-byte/react-calendar-timelineywqsimmon-antd@britishcouncil/formikbapel@brighte/react-native-paper@babylimon/react-calendar-timeline@braces/react-formbasic-react-formbeautiyrantdarminzeal-cube@annotation-studio/components@annotation-studio/plugin-core@ashishsaundekar/react-navigation@blkmarketco/components-library@aven-cloud/navigation-core@blueeast/bluerain-os@gankstergg/react-calendar-timelinecib-ui@getstation/theme@gem-mine/docsify-react-liveaaa-manager@whollacsek/unstated@ventureharbour/react-calendar-timeline@yued/formikantd-xantd-rtlyoantdantdtesttest@sdsma/react-calendar-timeline
0.3.0

5 years ago

0.2.3

6 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.6

6 years ago

0.1.5

6 years ago

0.1.4

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago