react-xstream-store
React components for connecting an xstream-store store to your components.
Installation
npm i react-xstream-store xstream-store xstream
Usage
react-xstream-store
exports a Provider
to make your xstream
store
available through context
, and exports a Consumer
and withStream
HOC to
access state in the store.
Provider
// App.js;; ; ; const App = <Provider store=store> <Counter /> </Provider>
Prop | Description |
---|---|
store | An xstream-store store object |
Consumer
A typical React context consumer that expects a function for children
,
returning bound action creators, state, and dispatch
from the store.
// Counter.js; ; const selector = { return count: statecount;};const actionMap = decrement: decrementAction increment: incrementAction const Counter = <Consumer selector=selector actions=actions> <div> <button onClick=decrement>-</button> count <button onClick=decrement>+</button> </div> </Consumer> ;
Prop | Type | Description |
---|---|---|
selector | fn: (state) => state | A function that receives the entire store's state. This is a good place to select only the state from the store your component requires. |
actions | obj: {key: actionCreator | action} | An object mapping keys to action creators. Action creators (functions which return actions) are automatically bound to xstream-store 's dispatch. |
withStream
A higher-order component and alternative to the Consumer
component for making
actions, state, and dispatch available within a component.
// Counter.js; ; const Counter = <div> <button onClick=decrement>-</button> count <button onClick=decrement>+</button> </div> const selector = { return count: statecount;};const actions = decrement: decrementAction increment: incrementAction selector actionsCounter;
Todo
- add examples
License
MIT