David Bashford
UI/Node Engineer
export const coolThings = createSelector(
[things],
(_things) => things.filter((thing) => thing.isCool)
);
A pure, memoized function that takes a set of inputs and returns an output
const cartItems = (state) => state.cart.items;
const getTotal = createSelector(
[cartItems],
(_cartItems) => {
return _cartItems.reduce((prev, item) => {
return prev + item.count * item.amount;
}, 0);
}
);
const inventoryItems = (state) => state.inventory.items;
const getInventory = createSelector(
[inventoryItems],
(_inventoryItems) => {
return _inventoryItems.map((item) => {
item.isSoldOut = item.amount === 0;
item.isAlmostSoldOut = item.amount > 0 && item.amount < 4;
item.isNoLongerAvailable = item.amount === -1;
return item;
});
}
);
const inputs = (_inputs) => _inputs;
const expensiveComputation = createSelector(
[inputs],
(_inputs) => {
console.log('COMPUTING');
return doSomethingToDataThatTakesALongTime(_inputs);
}
);
expensiveComputation(data); // COMPUTING
expensiveComputation(data); // no console log
const exampleActionCreator = (type) => {
return (dispatch, getState) => {
...
if (type == ACTION_TYPES.TOGGLE_HEADER) {
prefService.setPreferences(
'events',
getReconPreferences(getState())
)
}
...
};
};
By David Bashford