我正在使用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(),
}),
});
}
};
当我想涂填充剂时。例如,当我得到一个操作控制台时,我有这样一个数组:
操作控制台。有效负载=>[{property:〃StartDate:,值:";12.12.12"}{property:"结束日期:,值:";13.13.12"}{属性:"标题":,值:"测试"}{属性;"名称":,值:";name"}"]
我应该如何使StartDate和EndDate值处于状态。reportFilter.dateFilter并将其余值放入searchItem数组
请通过替换reducer方法来尝试此代码
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,
}),
});
}
};