从Redux阵列中仅删除一个元素不起作用

我试图做一个简单的功能,从数组中删除一个项目。它应该是动态的。

import { LOAD_PAGES, REMOVE_PAGE } from "./dynamicMenuActions";

export const initialState = { pages: [] };

export const dynamicMenuReducer = (state = initialState, action) => {
  switch (action.type) {
    case LOAD_PAGES:
      return {
        ...state,
        pages: [...state.pages, action.pages]
      };
    case REMOVE_PAGE:
      return {
        ...state,
        pages: [...state.pages.filter((p, i) => p.dataIndex !== action.pages[i].dataIndex)]
      };
    default:
      return state;
  }
};

export default dynamicMenuReducer;

那个行动:

export const removePageAction = (pages) => ({
  pages,
  type: REMOVE_PAGE
});

因此,在组件中,我这样称呼它:

  const deletePage = () => {
    const pagesCopy = [...pages];

    removePageAction(pagesCopy);
  };

现在,它将删除数组中的所有项目,而不仅仅是一个。

你能看到我做错了吗?

评论