Created
February 27, 2017 02:37
-
-
Save JonathonAshworth/25a91268a0e47e093398d0f8b4a5e4ec to your computer and use it in GitHub Desktop.
Reselect vs Vanilla
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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