Code Monkey home page Code Monkey logo

react-context-utils's Introduction

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')
);

react-context-utils's People

Contributors

iamstarkov avatar mathieuancelin avatar npmcdn-to-unpkg-bot avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.