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

v1.1.2

Published

Simple component and decorator to provide contexts to React components

Downloads

6

Readme

react-context-utils

build status

Utility lib to manipulate React context easily and use props as much as possible.

npm install react-context-utils

or

<script src="https://unpkg.com/react-context-utils/dist/react-context-utils.js"></script>

Provide a context

You just need to use the ContextProvider component to provide a context to you whole component tree. The context is a good way to provide some global services or actions to your components. It also make testing easier, if you put the provider at the root of the tree, so you can easily provide a test context to your components without changing them.

import React from 'react';
import ReactDOM from 'react-dom';
import { ContextProvider } from 'react-context-utils';

const context = {
  helloService: (who = 'World') => `Hello ${who}!`,
};

const App = React.createClass({
  render() {
    return (
      <h1>
        Hello Dude!
      </h1>
    );
  },
});

ReactDOM.render(
  <ContextProvider context={context}>
    <App />
  </ContextProvider>
  , document.getElementById('app')
);

Map context to components props

Now, if you want to use services from inside the context, you just need to map those services to you component props by using the higher order components pattern. You can define you component like

import React from 'react';
import { mapContextWith } from 'react-context-utils';

const Hello = React.createClass({
  propTypes: {
    service: React.PropTypes.func.isRequired,
    who: React.PropTypes.string.isRequired,
  },
  render() {
    return <span>{this.props.service(this.props.who)}</span>;
  },
});

function mapper(context) {
  return {
    service: context.helloService,
  };
}

export default mapContextWith(mapper)(Hello);

and then use it in your app

import React from 'react';
import ReactDOM from 'react-dom';
import { ContextProvider } from 'react-context-utils';

const context = {
  helloService: (who = 'World') => `Hello ${who}!`,
};

const App = React.createClass({
  render() {
    return (
      <h1>
        <HelloComponent who="Dude" />
      </h1>
    );
  },
});

ReactDOM.render(
  <ContextProvider context={context}>
    <App />
  </ContextProvider>
  , document.getElementById('app')
);

Multiple contexts

It is possible to deeply nest contexts with different names

import React from 'react';
import ReactDOM from 'react-dom';
import { ContextProvider, mapContextWith } from 'react-context-utils';

const contextA = { ... };
const contextB = { ... };

const Component = mapContextWith(c => ({ ... }), 'B')(...);
const OtherComponent = mapContextWith(c => ({ ... }), 'A')(...);

const App = React.createClass({
  render() {
    <ContextProvider context={contextA} ctxName="A">
      <div>
        <ContextProvider context={contextB} ctxName="B">
          <Component />
        </ContextProvider>
        <AnotherComponent />
        <OtherComponent />
      </div>
    </ContextProvider>
  }
});

ReactDOM.render(<App />, document.getElementById('app'));

you can also consume multiple contexts in one component by using

import { mapContextWith } from 'react-context-utils';

const Component = React.createClass(...);

export default mapContextWith([
  { mapper: c => c, name: 'default' },
  { mapper: c => c, name: 'secondary' },
])(Component);

Out of the box event bus

react-context-utils provides a simple event bus for the wrapped component tree.

It's pretty easy to use

import React from 'react';

import {
  ContextProvider,
  EventBusShape,
  mapContextWith
} from 'react-context-utils';

const Emitter = mapContextWith()(React.createClass({
  propTypes: {
    eventBus: EventBusShape,
  },
  emit() {
    this.props.eventBus.dispatch('events', 'Hello World');
  },
  render() {
    return (
      <button type="button" onClick={this.emit}>Emit</button>
    );
  },
}));

const Receiver = mapContextWith()(React.createClass({
  propTypes: {
    eventBus: EventBusShape,
  },
  getInitialState() {
    return {
      message: 'void',
    };
  },
  componentDidMount() {
    this.unsubscribe = this.props.eventBus.subscribe('events', payload => this.setState({ message: payload }));
  },
  componentWillUnmout() {
    this.unsubscribe();
  },
  render() {
    return (
      <div>
        <span>{this.state.message}</span>
      </div>
    );
  },
}));

const App = React.createClass({
  render() {
    return (
      <div>
        <Emitter />
        <Receiver />
      </div>
    );
  },
});

ReactDOM.render(
  <ContextProvider context={context}>
    <App />
  </ContextProvider>
  , document.getElementById('app')
);