A HOC (Higher Order Component) for Unstated Subscribe.
This tiny module was made to get a more functional way of using Unstated -- similar to redux's way of binding state to a component with connect, and also similar to and compatible with recompose.
function Counter() {
return (
<Subscribe to={[CounterContainer]}>
{counter => (
<div>
<button onClick={() => counter.decrement()}>-</button>
<span>{counter.state.count}</span>
<button onClick={() => counter.increment()}>+</button>
</div>
)}
</Subscribe>
);
}
const CounterView = ({ counter }) => (
<div>
<button onClick={() => counter.decrement()}>-</button>
<span>{counter.state.count}</span>
<button onClick={() => counter.increment()}>+</button>
</div>
);
const Counter = subscribeTo([CounterContainer], ['counter'])(CounterView);
import { compose } from 'recompose';
const Counter = compose(
subscribeTo([CounterContainer], ['counter'])
// other possible HOCs
)(({ counter }) => (
<div>
<button onClick={() => counter.decrement()}>-</button>
<span>{counter.state.count}</span>
<button onClick={() => counter.increment()}>+</button>
</div>
));
MIT