zelektree
v1.0.2
Published
Embed selectors in a Redux state tree.
Downloads
10
Readme
zelektree
Embed selectors in a Redux state tree.
This is a convenient way for components to share selectors that are used widely throughout an app.
Inspired by Baobab.
Install
$ yarn install zelektree
Usage
import { connect, createStore, } from 'redux';
import { embedSelectors, } from 'zelektree';
const initialState = { foo: null, };
const reducer = (state = initialState) => state;
const store = createStore(
reducer,
// pass as the `enhancer` argument to `createStore`
embedSelectors({
// key each selector by intended state key name
isFoo: (state) => !!state.foo,
}),
);
// state and derived state are synced and merged
const state = store.getState(); // { foo: null, isFoo: false, }
API
embedSelectors(selectors)
Takes an object map of selectors and returns a Redux StoreEnhancer.
The store enhancer refreshes the selectors each time getState
is called and
the result is merged with the returned state.
embedSelectors
expects the top-level state value to be an object. To optimize
performance, this expectation is not enforced with runtime checks.
selectors
Type: { [String]: (State) -> State }
An object map of selectors. The key for each selector is used as the state tree
key for that selector's output. A selector is called with a single argument, state
.
state
is the current state of the store.
embedSelectors
is interoperable with any selector that is soley a reduction of
state and not state and props, for example.
License
MIT © Max Hallinan