Reactjs Redux-如何将项添加到特定属性中
我真的很难做到这一点,我想在我的“binnacleNotesReducer”中的“binnacleNotes”属性中添加一个新项目,我尝试了几种在互联网上发现的方法,但效果不好 新注释要么被添加到主属性“binnaceNotesReducer”中,要么未被添加 我有以下redux结构: 请参考我在下面代码中的几次尝试:Reactjs Redux-如何将项添加到特定属性中,reactjs,redux,react-redux,Reactjs,Redux,React Redux,我真的很难做到这一点,我想在我的“binnacleNotesReducer”中的“binnacleNotes”属性中添加一个新项目,我尝试了几种在互联网上发现的方法,但效果不好 新注释要么被添加到主属性“binnaceNotesReducer”中,要么未被添加 我有以下redux结构: 请参考我在下面代码中的几次尝试: case ADD_NEW_NOTE: const { id, date, binnacle_note, responsibleState, attachments,
case ADD_NEW_NOTE:
const { id, date, binnacle_note, responsibleState, attachments, constructor_review, super_review, dro_review, timestamp } = payload;
const newNote = {
binnacleNotes: {
id,
date,
binnacle_note,
responsibleState,
attachments,
constructor_review,
super_review,
dro_review,
timestamp,
},
};
//return state.binnacleNotes.concat(newNote);
return {
...state.binnacleNotes,
binnacleNotes: {
...state.binnacleNotes,
id,
date,
binnacle_note,
responsibleState,
attachments,
constructor_review,
super_review,
dro_review,
timestamp,
},
};
不同的方法:
case ADD_NEW_NOTE:
const { id, date, binnacle_note, responsibleState, attachments, constructor_review, super_review, dro_review, timestamp } = payload;
const newNote = {
binnacleNotes: {
id,
date,
binnacle_note,
responsibleState,
attachments,
constructor_review,
super_review,
dro_review,
timestamp,
}
}
return state.binnacleNotes.concat(newNote);
在阅读了更多文档后,我找到了解决方案,希望这对任何人都有帮助:
case ADD_NEW_NOTE:
const { id, date, binnacle_note, responsible, attachments, constructor_review, super_review, dro_review, timestamp } = payload;
return {
binnacleNotes: [
...state.binnacleNotes,
{ id, date, binnacle_note, responsible, attachments, constructor_review, super_review, dro_review, timestamp },
],
};
看起来有效载荷就是纸条?因此,没有必要去破坏结构。此外,如果减速器中存在除binnacleNotes以外的任何属性,则需要复制它们。我想这里没有,但要记住。return{…state,binnacleNotes:[…state.binnacleNotes,action.payload]}是的,你说得对,很抱歉耽搁了。如果我只需要更新某些属性而不向对象添加新项,该怎么办?我一直在努力解决这个问题。