微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如何将对象的有效负载附加到由对象数组组成的状态,在 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 举报,一经查实,本站将立刻删除。