使用dva 的思考的一个问题,数组复制的必要

 *getTags({ payload }, { call, put }) {
            const response = yield call(getTags, payload);

            const arr = response.Data && response.Data.length > 0 ? response.Data : []
            arr.map(t => {
                if (t.Tags.length > 0) {
                    t.Tags.map(t2 => {
                        t2.isSelect = false
                    })
                }
            })
           
            yield put({
                type: 'querySuccess',
                payload: {
                    tagList: arr, // 作为源数据
                    tagQRList: arr,
                    tagMtList: arr,
                },
            });
            return response
        },

问题:tagList、tagQRList、tagMtList 将会共用一个内存地址,使用引用改变值时会影响另外2个,也即不能达到我们要的效果,以下办法没生效

            yield put({
                type: 'querySuccess',
                payload: {
                    tagList: arr, 
                    tagQRList: [].concat(arr),
                    tagMtList: [].concat(arr),
                },
            });

以上是浅拷贝的方法:https://www.cnblogs.com/cccj/p/8660888.html

因为我的是需要深拷贝https://blog.csdn.net/qq_37268201/article/details/80448848

以下写法可以解决问题

  yield put({
                type: 'querySuccess',
                payload: {
                    tagList: arr, // 作为源数据
                    tagQRList: JSON.parse(JSON.stringify(arr)), // 深拷贝
                    tagMtList: JSON.parse(JSON.stringify(arr)), // 深拷贝
                },
            });

猜你喜欢

转载自www.cnblogs.com/ww01/p/11800320.html
dva