问题:
handleSelectRows = (rows,int) => {
const { dispatch,disease:{DSdata,selectDiseaseRows,DSNoData} } = this.props;
if (int===0) {
dispatch({
type: 'disease/setStates',
payload:{
selectDiseaseRows:selectDiseaseRows.length===0?rows:selectDiseaseRows.concat(rows),
DSNoData:DSNoData.length===0?rows:DSNoData.concat(rows)
},
});
开始是直接将数组复制给selectDiseaseRows和DSNoData,在对selectDiseaseRows中的元素进行改变时,DSNoData中的元素也在变化,所以判断出,是因为数组是引用类型,直接复制其实是复制的“指针”
解决:
handleSelectRows = (rows,int) => {
const { dispatch,disease:{DSdata,selectDiseaseRows,DSNoData} } = this.props;
if (int===0) {
const row=rows.map(i=>{return{...i}})
const row2=rows.map(i=>{return{...i}});
dispatch({
type: 'disease/setStates',
payload:{
selectDiseaseRows:selectDiseaseRows.length===0?row:selectDiseaseRows.concat(row),
DSNoData:DSNoData.length===0?row2:DSNoData.concat(row2)
},
});
1.slice(),浅拷贝,如果数组是[1,2,3]这样的就可以拷贝,如果数组是[{a:1},{a"2}],数组里是object,拷贝还是复制的指针
2.Object.assign(),也是浅拷贝
最终解决方法,循环解构数组里的元素对象{...i},将它保存在另一个数组里,这样就不是拷贝的指针了