记住基于单个输入选择器而不是全部输入的重新选择选择器输出

时间:2020-05-21 18:30:56

标签: javascript redux reselect

我有一个Reselect选择器,它将选择的ID数组映射到规范化存储中的对象。

const activeObjectsSelector = createSelector(
  state => state.activeIds,
  state => state.objects.byId,
  (activeIds, objectsById) => activeIds.map(id => objectsById[id])
)

问题在于,由于state.objects.byId的值发生了更改,因此它会在每次将新对象添加到规范化存储时破坏缓存并重新运行。我唯一关心的是清除缓存的更改是state.activeIds的值。这可能吗?

1 个答案:

答案 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>