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 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-dividable-context

v1.2.2

Published

An efficient way of having one React Context instead of multiple providers, without unnecessary re-rendering

Downloads

3

Readme

react-dividable-context

GitHub package version NPM Donate

Demo available on GitHub Pages.

API

Similar to the official context API added in React 16.3, which is what gets used by this module internally.

// Check whether two values are the same
// Uses `JSON.stringify(a) === JSON.stringify(b)` by default
type EqualsFunction<T> = (a: T, b: T) => boolean;

// Create a new context
const context = create(defaultValue: T, EqualsFunction<T>);

// The provider/consumer, similar to the ones from React Context
const provider = <context.Provider/>; // See note below!
const consumer = <context.Consumer/>;

// A consumer that only updates if the given field(s) update
const partialConsumer = context.getConsumer(['fieldA', 'fieldB']);

// Otherwise, we can also use the "main" one with the "keys" property
const listenToFieldA = <context.Consumer keys={['fieldA']} />;

// We can mix a partial and the "keys" prop too
// This one will listen to fieldA, fieldB and fieldC
const listenToSeveral = <partialConsumer keys={['fieldC']} />;

// We can also check for "sub keys"
// This'll listen to fieldA and fieldB.subA
const listenForSubKey = <context.Consumer keys={['fieldA', ['fieldB', 'subA']]} />;

Important: Unlike the official Provider, the one exposed by context prevents its children from updating. This allows wrapping your app in the provider and updating the value prop, without it having re-rendering the whole app. All (descendant) consumers will still update.

const context = create({});
// ...
    <context.provider value={{}}>
        <p>This will never update! {Date.now()}</p>
        <context.consumer>
            {data => <p>This one updates, though. {Date.now()}</p>}
        </context.consumer>
    </context.provider>
// ...

Usage

npm install --save react-dividable-context;
# or
yarn add react-dividable-context;
import * as React from 'react';
import create from 'react-dividable-context';

// Our demo data
interface ITest {
  abc: string;
  def: number;
  idk: number[];
}

// Create the context, similar to React.createContext
const contexter = create({ abc: 'ABC', def: 123, idk: [0] });
const ConsumerABC = contexter.getConsumer(['abc']);
const ConsumerMixed = contexter.getConsumer(['abc', 'def']);

interface IAppState {
  test: ITest;
}
class App extends React.Component<any, IAppState> {
  constructor(props: any, context?: any) {
    super(props, context);
    this.state = { test: { abc: 'ABC', def: 123, idk: [0] } };
  }
  public render() {
      return (
          <context.provider value={this.state.test}>
            <context.consumer>
                {test => <p>Something changed! {Date.now()}</p>}
            </context.consumer>
            <ConsumerABC>
                {test => <p>Field 'abc' changed! {Date.now()}</p>}
            </ConsumerABC>
            <ConsumerMixed>
                {test => <p>Field 'abc' and/or 'def' changed! {Date.now()}</p>}
            </ConsumerMixed>
          </context.provider>
      )
  }
}