Skip to content
This repository has been archived by the owner on Aug 10, 2019. It is now read-only.

fabiospampinato/unstated-with-containers

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Unstated With Containers

Higher-Order Component for subscribing to containers.

Simplifies your components, removing the need to constantly use the Subscribe component.

Install

$ npm install --save unstated-with-containers

Usage

import * as React from 'react';
import {Container} from 'unstated';
import withContainers from 'unstated-with-containers';

class CounterContainer extends Container {
  state = { value: 0 };
  increment = () => {
    this.setState({ value: this.state.value + 1 });
  }
  decrement = () => {
    this.setState({ value: this.state.value - 1 });
  }
}

const App = withContainers ( CounterContainer )(
  ({ containers: [counter] }) => ( // props.containers[0] === props.container
    <>
      <p>{counter.state.value}</p>
      <button onClick={counter.increment.bind ( counter )}>Increment</button>
      <button onClick={counter.decrement.bind ( counter )}>Decrement</button>
    </>
  )
);

Related

License

MIT © Fabio Spampinato

About

Higher-Order Component for subscribing to containers.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published