npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

context-api-polyfill

v1.4.2

Published

Polyfill React 15 with the new context api

Readme

context-api-polyfill

This library polyfills the React < 16.0.0 with the new context api. Thus, it is possible to use this feature and still have releases to react 15 as non-breaking change.

Caveat

The context api is based on a Provider and a Consumer which obey the closure defined by the browser DOM Tree. In this matter, it can be seen as a closure isolating each provider that is being wrapper by another provider.

To mimic the closure created by the DOM Tree during render, it is being used the stack data structure. Everytime when a provider in the virtual DOM Tree runs its lifecycle methods componentWillMount or componentWillReceiveProps, it is being added to the stack. Also removed when componentDidMount or componentDidUpdate.

I highly recommend setting the initial value to the context created and use that to set value to the component that is resposible to feed the provider with value.

import React from "react";
import "context-api-polyfill";

// Here is your initial value
const INITIAL_VALUE = "red";
// As expected by the createContext function, you pass down the value that is going to be set as default.
const BackgroundContext = React.createContext(INITIAL_VALUE);
// Also, set this value as initialValue to your context object
BackgroundContext.initialValue = INITIAL_VALUE;

export default BackgroundContext;

Doing it so, you can use as:

import React, { Component } from "react";
import PropTypes from "prop-types";
import BackgroundContext from "./BackgroundContext";

export default class Switcher extends Component {
  static propTypes = {
    children: PropTypes.node.isRequired
  };

  state = {
    // Setting initial value to the state
    backgroundColor: BackgroundContext.initialValue
  };

  toggleBgColor = () => {
    this.setState(({ backgroundColor }) => ({
      backgroundColor: backgroundColor === "red" ? "blue" : "red"
    }));
  };

  render() {
    const { children } = this.props;
    const { backgroundColor } = this.state;

    return (
      <div>
        <button type="button" onClick={this.toggleBgColor}>
          Toggle Bg Color
        </button>
        {/* feeding back the value according to the toggle color value */}
        <BackgroundContext.Provider value={backgroundColor}>
          {children}
        </BackgroundContext.Provider>
      </div>
    );
  }
}

For more information, check out the DEV folder in this project GitHub Repo.

API

Test

npm test