All Projects → StringEpsilon → mini-create-react-context

StringEpsilon / mini-create-react-context

Licence: MIT License
(A smaller) polyfill for the react context API

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to mini-create-react-context

Resize Observer
Polyfills the ResizeObserver API.
Stars: ✭ 540 (+1488.24%)
Mutual labels:  polyfill, ponyfill
Scroll Into View If Needed
Element.scrollIntoView ponyfills for things like "if-needed" and "smooth"
Stars: ✭ 811 (+2285.29%)
Mutual labels:  polyfill, ponyfill
Unfetch
🐕 Bare minimum 500b fetch polyfill.
Stars: ✭ 5,239 (+15308.82%)
Mutual labels:  polyfill, ponyfill
Abortcontroller Polyfill
Polyfill for the AbortController DOM API and abortable fetch (stub that calls catch, doesn't actually abort request).
Stars: ✭ 273 (+702.94%)
Mutual labels:  polyfill, ponyfill
o9n
🖥 A screen.orientation ponyfill
Stars: ✭ 55 (+61.76%)
Mutual labels:  polyfill, ponyfill
Promise Fun
Promise packages, patterns, chat, and tutorials
Stars: ✭ 3,779 (+11014.71%)
Mutual labels:  polyfill, ponyfill
Clipboard Polyfill
📋 Simple copying on the web, with maximum browser compatibility.
Stars: ✭ 748 (+2100%)
Mutual labels:  polyfill, ponyfill
Create React Context
Polyfill for the proposed React context API
Stars: ✭ 689 (+1926.47%)
Mutual labels:  polyfill, context
Resize Observer Polyfill
A polyfill for the Resize Observer API
Stars: ✭ 1,530 (+4400%)
Mutual labels:  polyfill, ponyfill
Css Vars Ponyfill
Client-side support for CSS custom properties (aka "CSS variables") in legacy and modern browsers
Stars: ✭ 1,166 (+3329.41%)
Mutual labels:  polyfill, ponyfill
web-streams-polyfill
Web Streams, based on the WHATWG spec reference implementation
Stars: ✭ 198 (+482.35%)
Mutual labels:  polyfill, ponyfill
Tickedoff
Tiny library (<200B gzip) for deferring something by a "tick"
Stars: ✭ 213 (+526.47%)
Mutual labels:  polyfill, ponyfill
fromentries
Object.fromEntries() ponyfill (in 6 lines)
Stars: ✭ 62 (+82.35%)
Mutual labels:  polyfill, ponyfill
Fakeindexeddb
A pure JS in-memory implementation of the IndexedDB API
Stars: ✭ 373 (+997.06%)
Mutual labels:  polyfill, ponyfill
Ponyfill
🦄 Like polyfill but with pony pureness
Stars: ✭ 945 (+2679.41%)
Mutual labels:  polyfill, ponyfill
React Af
Allows you to code using certain React.next features today! Perfect for component library maintainers.
Stars: ✭ 143 (+320.59%)
Mutual labels:  polyfill, context
Core Js
Standard Library
Stars: ✭ 15,854 (+46529.41%)
Mutual labels:  polyfill, ponyfill
eval-estree-expression
Safely evaluate JavaScript (estree) expressions, sync and async.
Stars: ✭ 22 (-35.29%)
Mutual labels:  context
form-data
Spec-compliant FormData implementation for Node.js
Stars: ✭ 73 (+114.71%)
Mutual labels:  ponyfill
appHistory
A polyfill for the AppHistory proposal
Stars: ✭ 21 (-38.24%)
Mutual labels:  polyfill

mini-create-react-context

npm install size npm bundle size npm

(A smaller) Polyfill for the React context API

Install

npm install mini-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 '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 (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>

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
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].