ドラッグでDropを使用してdivのReact/Reduxを再配置しようとしていますが、私は説明できない奇妙な振る舞いがあります。私は次の減速器を持っています。(可読性のための縮小版)変更する前にconsole.logが変更された状態を表示します
コード中央付近に5つの "console.log"があります。 の状態またはの構造体を記録すると、クロムコンソールはすでに並べ替えられたバージョンを印刷します。どうして?
export default function reducer(
state={
structures: [],
elementUniqueCounter: 0,
hoveredElement: null,
DnD: {
dragContent: null,
dragOverContent: null,
dragContentTop: true,
dragStructure: null,
dragOverStructure: null,
dragStructureTop: true,
content: null,
mousepositon: {}
}
}, action) {
let DnD = Object.assign({}, state.DnD);
let structs = state.structures.slice();
switch (action.type) {
case "STOP_DRAG_CONTENT":
let cindex_source;
let index_source;
let cindex_target;
let index_target;
let foundTarget = false;
let foundSource = false;
structs.map(function (struct, index) {
struct.content.map(function (content, cindex) {
if(content.id === DnD.dragOverContent.props.id) {
cindex_target = cindex;
index_target = index;
foundTarget = true;
}
if(content.id === DnD.dragContent.props.id) {
cindex_source = cindex;
index_source = index;
foundSource = true;
}
});
});
console.log(state);
console.log(index_source);
console.log(cindex_source);
console.log(index_target);
console.log(cindex_target);
if(index_source !== undefined && cindex_source !== undefined && index_target !== undefined && cindex_target !== undefined) {
let copy = structs[index_source].content.slice(cindex_source, cindex_source+1)[0];
copy.content = DnD.content;
structs[index_source].content.splice(cindex_source, 1);
if (DnD.dragContentTop) {
structs[index_target].content.splice(cindex_target+1, 0, copy);
} else {
structs[index_target].content.splice(cindex_target, 0, copy);
}
}
DnD = {
dragContent: null,
dragOverContent: null,
dragContentTop: true,
dragStructure: null,
dragOverStructure: null,
dragStructureTop: true,
content: null,
mousepositon: {}
};
return {...state, DnD: DnD, structures: structs};
}
return state
}
私はスライスでコピー(参照ではない)を作ることができると思いました。だから私はスイッチの前にそれを使ったのです(structs = state.structures.slice();) – Tiega
はい、 'slice'は' shallow'コピーを返すだけで、ネストされたプロパティを変更しています。 'slice'を使う代わりに' JSON.parse(JSON.stringify(state.structures)) 'でオブジェクトを複製してみてください。このメソッドは結果のオブジェクトから 'undefined'値を削除することに注意してください。 –
私は、ネストされたプロパティが問題であることを知りませんでした。ありがとう – Tiega