我的減速器如下:添加功能,減速機
const removeFilter = (state, name) => {
return state.filter(f => f.name !== name);
};
export default function addRemoveFilterReducer(state = [], action) {
switch (action.type) {
case ADD_FILTER:
if(!state.some(i => i.name === action.searchFilter.name))
return [...state, {name: action.searchFilter.name, values: [action.searchFilter.values]}];
else
return [...state.filter(f => f.name !== action.searchFilter.name), {name: action.searchFilter.name, values: [action.searchFilter.values]}];
case REMOVE_FILTER:
return [...state.filter(f => f.name !== action.searchFilter.name)];
break;
default:
return state;
}
}
在的add_filter情況下其他國家和REMOVE_FILTER情況下,我有相同的代碼:...state.filter(f => f.name !== action.searchFilter.name)
。
我創建了一個函數使用該代碼刪除過濾器。我現在如何在我的情況下使用這個功能?
我試着用return [removeFilter(state, action.searchFilter.name, {name: action.searchFilter.name, values: [action.searchFilter.values]}];
處於ADD_FILTER狀態的else狀態,但它不起作用。
有什麼建議嗎?
UPDATE
函數調用:
return [removeFilter(state, action.searchFilter.name, ......];
您忘記了函數中的方括號和擴展運算符。 –
還有更多,你不需要* REMOVE_FILTER *中的最後一個* break *指令,因爲你總是從函數返回。甚至* ADD_FILTER *中的* else *都可以被刪除。至於如何工作的開關,如果有東西跳*如果*,比移動到下一個* case *指令。 –
我改變了它,問題依然存在。如果reducer沒有函數,我在控制檯日誌中獲得'[object]'但是如果我使用函數有或沒有變化,我得到'[Array [0],Object]' – Boky