如何将对象的有效负载附加到由对象数组组成的状态,在 reducer 中使用扩展运算符..语法问题

问题描述

我正在尝试附加在一个屏幕中输入并显示在另一个屏幕上的数据负载。数据作为对象数组保存。您如何使用扩展运算符构建您的减速器,以便保留先前的状态?我已经尝试了两到三种方法,并在减速器代码的注释部分给出了错误。什么是正确的语法?

减速器代码

const initial = {
    reports: []
    
};

export default function attendanceReducer(state = initial,action){
     switch(action.type) {
         case 'UPDATE_REPORTS':
            return {
                ...state,//reports: action.payload,// no persistence
                reports: [...state.reports,...action.payload.reports] //TypeError: Invalid attempt to spread non-iterable
                //reports: [state.reports,action.reports] //Returns nested arrays with nesting depth increasing for each action call of the form [[[]][]]
                
             };
        
         default:
             return state;    



     }
}  

对象:

    var reports = [];
            for(i=0;i<selectedItems.length;i++){
                var attendanceObj = {};
                attendanceObj["key"]=selectedItems[i];
                attendanceObj["name"]=selectedCatArray[i][0];
                attendanceObj["date"] = this.state.currDate;
                attendanceObj["qty1"]=1;
                attendanceObj["qty2"]=1;
                reports.push(attendanceObj);
            }
        
console.log('action.payload',action.payload):

action.payload {"reports": [{"date": "2021-03-01","key": "603a369d167abd00176bdd8f","name": "Plumbiggd","qty1": 2,"qty2": 1},{"date": "2021-03-01","key": "603fd63bfe94590017b93921","name": "电","qty1": 1,"qty2": 2}]}

console.log('this.state.report',this.state.report ):

 this.state.report [{"date": "2021-03-02","key": "603a369d167abd00176bdd8f","qty2": 1},{"date": "2021-03-02","key": 
 "603c97c742b7b9001755d63b","name": 
"Plumbing","qty2": 3}]

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)