我正在使用一个useReducer,这就是我所拥有的:
const initialState = {
optionalFilter: {
take: Math.round((window.innerHeight - 330) / 34),
page: 1,
},
reportFilter: {
searchItem:[],
dateFilter: {
StartDate:new Date(new Date().setDate(new Date().getDate() - 3100)),
EndDate:new Date(),
},
},
};
const reducer = (state, action) => {
switch (action.type) {
case "changeFilter":
console.log("action",action.payload)
return updateObject(state, {
optionalFilter: updateObject(state.optionalFilter, {
page: 1,
}),
reportFilter: updateObject(state.reportFilter, {
searchItem: action.payload,
}),
reportFilter: updateObject(state.reportFilter.dateFilter, {
StartDate:new Date(new Date().setDate(new Date().getDate() - 3100)),
EndDate:new Date(),
}),
});
}
};当我想应用填充.For示例时,当我得到一个操作控制台时,我有这样一个数组:
控制台action.payload==>{property:"StartDate:,value:"12.12.12"}{property:"EndDate:,value:“13.13.12”}{property:“StartDate:,value:”StartDate“}{property:”name“:,value:"name"} }
如何在state.reportFilter.dateFilter中使用StartDate和EndDate值,以及如何将其余的值放在searchItem数组中
发布于 2021-07-04 09:15:18
请通过替换减速机方法来尝试此代码。
const reducer = (state, action) => {
switch (action.type) {
case "changeFilter":
console.log("action",action.payload)
return updateObject(state, {
optionalFilter: updateObject(state.optionalFilter, {
page: 1,
}),
reportFilter: updateObject(state.reportFilter, {
searchItem: action.payload.filter(val => val.property !== "StartDate" && val.property !== "EndDate");,
}),
reportFilter: updateObject(state.reportFilter.dateFilter, {
StartDate:action.payload.find(val => val.property === "StartDate").value,
EndDate:action.payload.find(val => val.property === "EndDate").value,
}),
});
}
};https://stackoverflow.com/questions/68242862
复制相似问题