Socket
Book a DemoInstallSign in
Socket

react-dividable-context

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-dividable-context

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

1.2.2
latest
npmnpm
Version published
Maintainers
1
Created
Source

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

FAQs

Package last updated on 27 Jun 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.