Skip to content

Instantly share code, notes, and snippets.

@JonathonAshworth
Created February 27, 2017 02:37
Show Gist options
  • Save JonathonAshworth/25a91268a0e47e093398d0f8b4a5e4ec to your computer and use it in GitHub Desktop.
Save JonathonAshworth/25a91268a0e47e093398d0f8b4a5e4ec to your computer and use it in GitHub Desktop.
Reselect vs Vanilla
import { createSelector } from 'reselect';
const shopItemsSelector = state => state.shop.items;
const taxPercentSelector = state => state.shop.taxPercent;
const subtotalSelector = createSelector(
shopItemsSelector,
items => items.reduce((acc, item) => acc + item.value, 0)
);
const taxSelector = createSelector(
subtotalSelector,
taxPercentSelector,
(subtotal, taxPercent) => subtotal * (taxPercent / 100)
);
export const totalSelector = createSelector(
subtotalSelector,
taxSelector,
(subtotal, tax) => ({ total: subtotal + tax })
);
const shopItemsSelector = state => state.shop.items;
const taxPercentSelector = state => state.shop.taxPercent;
const subtotalSelector = state =>
shopItemsSelector(state).reduce((acc, item) => acc + item.value, 0);
const taxSelector = state =>
subtotalSelector(state) * (taxPercentSelector(state) / 100);
export const totalSelector = state =>
({ total: subtotalSelector(state) + taxSelector(state)});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment