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-multi-context

v1.1.2

Published

Manage multiple contexts with a single React component.

Downloads

15

Readme

React Multi-Context Tweet

Manage multiple contexts with a single React component.

version minified size minzipped size downloads build

Install

  • npm install react-multi-context --save or
  • yarn add react-multi-context

Test

  • npm run test or
  • yarn test

Use

import createMultiContext from 'react-multi-context';
export const MyMultiContext = createMultiContext();

Create the context by importing and executing createMultiContext wherever you want to create context. Then, import that multi-context instance as needed.

Use the set prop to set a context's value. Changing the value on a key-value pair in a context will cause all getters for that key to re-render.

Use the get prop to get a context's value. Using this prop will execute the children render prop by passing the corresponding values of the context as the parameters.

Example

// Parent.js
import createMultiContext from 'react-multi-context';

// Context to provide to children.
export const ParentContext = createMultiContext();

export default class Parent extends React.Component {
  render() {
    return (
      <ParentContext
        set={{
          cost: 10,
          project: {
            name: 'React Multi-Context',
            version: 1.0
          },
          user: 'Charles'
        }}
      >
        <Child />
      </ParentContext>
    );
  }
}
// Child.js
// Get the context provided from the parent
import { ParentContext } from './Parent';

export default class Child extends React.Component {
  render() {
    return (
      <ParentContext get={[ 'project', 'user' ]}>
        {(project, user) =>

          /* This is a demo of React Multi-Context v1.0 by Charles! */
          <p>This is a demo of {project.name} v{project.version} by {user}!</p>
        }
      </ParentContext>
    );
  }
}

Example (Shorter)

// Parent - writes A and B
const Parent = () =>
  <MultiContextInstance set={{ a: 1, b: 2 }}>
    <Child1 />
    <Child2 />
    <Child3 />
  </MultiContextInstance>;
// Child1 - reads A
// Note: Each value is its own context, which is what makes this MULTI-context.
const Child1 = () =>
  <MultiContextInstance get={[ 'a' ]}>
    {(a) => `The value of A is ${a}!`}
  </MultiContextInstance>;
// Child2 - reads A and B
// Note: Reading multiple values will trigger a re-render if any one read value changes.
const Child2 = () =>
  <MultiContextInstance get={[ 'a', 'b' ]}>
    {(a, b) => `The value of A+B is ${a + b}!`}
  </MultiContextInstance>;
// Child3 - reads B and A
// Note: The order of the get prop corresponds to the order of the function parameters.
const Child3 = () =>
  <MultiContextInstance get={[ 'b', 'a' ]}>
    {(b, a) => `The value of A+B is ${a + b}!`}
  </MultiContextInstance>;

Default Values

You may pass an object of default values for the contexts as a parameter to createMultiContext or via the default prop.

const MyMultiContext = createMultiContext({ a: 0, b: 0 });

or

<MyMultiContext
  default={{ a: 0, b: 0 }}
  set={{ a: 1 }}
>
  <MyMultiContext get={[ 'b' ]}>
    {b => 'I predict that B equals zero: ' + b}
  </MyMultiContext>
</MyMultiContext>

You do not need to do both.

MultiContext.with

MultiContextInstance.with(...multiContextKeys)(Component) will bind the multiContextKeys of MultiContextInstance to the props of Component.

import React from 'react';
import { SomeMultiContext } from './some-component';

class MyComponent extends React.PureComponent {
  render() {
    return <div children={'My name is ' + this.props.name + ', and I am ' + this.props.age + '!'} />;
  }
}

// Binds the MultiContext's `name` property to MyComponent's `name` prop.
export default SomeMultiContext.with('name', 'age')(MyComponent);