【发布时间】:2020-02-08 07:28:59
【问题描述】:
我在 redux 中有以下状态对象:
console.log({
jobOffers: {
filters: {
employments: [],
careerLevels: [],
jobTypeProfiles: [],
cities: [],
countries: [],
searchTerm: '',
currentPage: 1,
pageSize: 5
}
}
});
我想将数组就业设置为新的。
那是我的 redux reducer:
export const reducer = (state = initialStateData, action) => {
switch (action.type) {
case Action.SET_ARR_FILTER:
{
const newNestedState = {
...state[action.key],
[action.key]: action.value,
};
return { ...state,
[action.key]: newNestedState
};
}
default:
return state;
}
};
动作:
export const SET_ARR_FILTER = 'SET_ARR_FILTER';
export const setEmployment = employment => ({
type: SET_ARR_FILTER,
key: 'employments',
value: employment,
});
但是在调用 reducer 后我的对象看起来像这样:
console.log({
employments: {
employments: ['HelloWorld']
},
})
这里有什么问题?
【问题讨论】:
-
如果您要使用 A 和 B 发送
setEmployment两次,您希望结果仅包含 B(即employments: ['B'])还是同时包含 A 和 B(employments: ['A', 'B'])? -
它应该包含两个值
标签: javascript redux spread-syntax