Last active
April 14, 2018 12:26
-
-
Save smagch/a015d104a783dee766735a9ad923b076 to your computer and use it in GitHub Desktop.
reselect test
This file contains 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 { createSelector } = require('reselect'); | |
const ids = [1,2,3]; | |
const byIds = { | |
1: { | |
name: 'one' | |
}, | |
2: { | |
name: 'two' | |
}, | |
3: { | |
name: 'three' | |
} | |
}; | |
const selector = createSelector( | |
(state) => state.ids, | |
(state) => state.byIds, | |
(ids, byIds) => ids.map(id => byIds[id]) | |
); | |
const state1 = { ids, byIds }; | |
const items = selector(state1); | |
const state2 = { | |
ids, | |
byIds: { | |
...byIds, | |
'3': { | |
name: 'THREE' | |
} | |
} | |
}; | |
console.log(items === selector(state1)); // true | |
console.log(items === selector(state2)); // false | |
console.log(items === selector(state1)); // false, cache expired | |
console.log(selector(state2)); // [ { name: 'one' }, { name: 'two' }, { name: 'THREE' } ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment