combineSelectors(selectors)
This module follows the patterns set out by redux with combineReducers
. It allows you to colocate your selectors with the reducer state they work on and simplifies their usage. It follows a pattern outlined by Dan Abramov in a free egghead.io lesson here
The basic idea is to use combineSelectors
everywhere you use combineReducer
as you progressively build up your store state. This allows a convenient way to access more complicated derived state (provided through selector functions) following your store state.
Example:
export const selectors = combineSelectors({
potato: potatoSelectors,
tomato: tomatoSelectors
});
// Like combineReducers, this will produce an object where the selectors are called with the section of the redux state it's keyed by
{
potato: {
// ... potato selectors
},
tomato: {
// ... tomato selectors
},
}
The access of your selectors now mirrors your redux state.
Example:
reducers/todos.js
{ } // Selectorsconst getTodo = stateid;const getAllIds = Object;
reducers/counter.js
{ } // Selectorsconst getCount = state;
reducers/index.js
todos counter const selectors =
App.js
let store = console// {// todos: [],// counter: 0// } console// {// todos: function() {...},// counter: function() {...}// } console// [] console// 0