unstated-with-containers
TypeScript icon, indicating that this package has built-in type declarations

1.1.6 • Public • Published

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

Package Sidebar

Install

npm i unstated-with-containers

Weekly Downloads

1

Version

1.1.6

License

MIT

Unpacked Size

9.33 kB

Total Files

8

Last publish

Collaborators

  • fabiospampinato