我有一个Reselect选择器,它将选择的ID数组映射到规范化存储中的对象。
const activeObjectsSelector = createSelector(
state => state.activeIds,
state => state.objects.byId,
(activeIds, objectsById) => activeIds.map(id => objectsById[id])
)
问题在于,由于state.objects.byId
的值发生了更改,因此它会在每次将新对象添加到规范化存储时破坏缓存并重新运行。我唯一关心的是清除缓存的更改是state.activeIds
的值。这可能吗?
答案 0 :(得分:0)
您可以为此使用defaultMemoize的reselect来将地图的结果传递给一个被记忆的函数,以将地图的各项作为参数来记忆地图的结果:
const { createSelector, defaultMemoize } = Reselect;
const state = {
activeIds: [1, 2],
objects: {
1: { name: 'one' },
2: { name: 'two' },
3: { name: 'three' },
},
};
const selectActiveIds = (state) => state.activeIds;
const selectObjects = (state) => state.objects;
const createMemoizedArray = () => {
const memArray = defaultMemoize((...array) => array);
return (array) => memArray.apply(null, array);
};
const createSelectAcitiveObjects = () => {
const memoizedArray = createMemoizedArray();
return createSelector(
selectActiveIds,
selectObjects,
(ids, objects) =>
memoizedArray(ids.map((id) => objects[id]))
);
};
const selectAcitiveObjects = createSelectAcitiveObjects();
const one = selectAcitiveObjects(state);
console.log('one is:',one);
const two = selectAcitiveObjects(state);
console.log('one is two', two === one);
const newState = { ...state, activeIds: [1, 2, 3] };
const three = selectAcitiveObjects(newState);
console.log('three is two', three === two);
<script src="https://cdnjs.cloudflare.com/ajax/libs/reselect/4.0.0/reselect.min.js"></script>
<div id="root"></div>