Redux Reselect重新撰写时不更新?

时间:2020-06-11 10:02:44

标签: javascript redux reselect

我有一个工作正常的Redux重选选择器:

// in reselect1.js

const getInputs = state => state.inputs;

export default createSelector(
  getInputs,
  inputs => {
    const { thing } = inputs;
    const items = inputs.items
      .map(item => item.text)
      .filter(item => item);
    return {
      thing,
      items
    };
  },
);

在另一个选择器中,如果我重复执行逻辑以获取inputsWithoutIds中没有ID的输入,则可以使用它:

// in reselect2.js

const inputsWithoutIds = state => {
  const { thing } = state.inputs;
  const items = state.inputs.items
    .map(item => item.text)
    .filter(item => item);
  return {
    thing,
    items
  };
};

const getSaved = state => state.saved;

export default createSelector(
  getSaved,
  inputsWithoutIds,
  (saved, inputs) => {
    const isSaved = saved.filter(saved => {
      return _isEqual(saved, inputs);
    }).length;
    return {
      isSaved,
    };
  },
);

但是,如果我尝试重用我的第一个选择器,那么当state.inputs.items更改时,将不会获取新值。因此,isSaved的值在应有的时间不变。

// in reselect2.js

import inputsWithoutIds from './reselect1'

const getSaved = state => state.saved;

export default createSelector(
  getSaved,
  inputsWithoutIds,
  (saved, inputs) => {
    const isSaved = saved.filter(saved => {
      return _isEqual(saved, inputs);
    }).length;
    return {
      isSaved,
    };
  },
);

我在文档中看到这些功能是可组合的,所以我不确定自己在做什么错吗? https://github.com/reduxjs/reselect

0 个答案:

没有答案