意外的redux .filter结果



直接回答问题,我的redux存储中有一个过滤器列表,其中有一些过滤器将被添加和删除。

添加过滤器如下:

case ADD_FILTER:
      return {
        ...state,
        filters: [
          ...state.filters,
          action.filter
        ]
      };

按预期工作。但是当删除过滤器时:

case REMOVE_FILTER:
      return {
        ...state,
        filters: state.filters.filter(
          filter => filter.type !== action.filter.type && filter.name !== action.filter.name
        )
      };

将从我的列表中删除所有过滤器!

下面是过滤器的示例表示:

{
    {
        type: 'sampleType1'
        name: 'value1'
    },
    {
        type: 'sampleType2'
        name: 'value1'
    },
    {
        type: 'sampleStatus1'
        name: 'value2'
    },
    {
        type: 'sampleStatus2'
        name: 'value3'
    },
}

和行动:

export function addFilter(filter) {
  return (dispatch) => {
    dispatch({
      type: ADD_FILTER,
      filter
    });
    dispatch(load());
  };
}

export function removeFilter(filter) {
  console.log(filter);
  return (dispatch) => {
    dispatch({
      type: REMOVE_FILTER,
      filter
    });
    dispatch(load());
  };
}

我怀疑.filter()函数一定有问题。

我希望我已经解释了一切。

如果您只想删除指定的过滤器,则应该使用or ||操作符:

filter => filter.type !== action.filter.type || filter.name !== action.filter.name

最新更新