reactjs - Redux - 如何将项目添加到某些属性中
问题描述
我真的很挣扎,我想在我的“binnacleNotesReducer”中的“binnacleNotes”属性中添加一个新项目,我尝试了几种我在互联网上发现的方法,但效果不佳。
新注释要么被添加到主属性“binnaceNotesReducer”中,要么不被添加。
我有以下redux结构:
请参考我在下面代码中的几次尝试:
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 },
],
};
推荐阅读
- c# - 如何搜索数据库项的子字符串?
- memory - 在 apache camel 中使用 rabbitmq 处理大文件/消息时减少内存使用
- sql - 错误:对于参数类型的函数 IF 没有匹配的签名:BOOL。支持的签名:IF(BOOL, ANY, ANY)
- javascript - 在嵌套数组中推送数据,猫鼬
- node.js - MongoDB 给出的结果字段值不等于查询对象的值
- php - Eloquent Model 的 create 方法在哪里?
- python - 中断连接的列表或数据框,python
- java - 在 OAuth2 中过滤或处理 AuthenticationException
- mocking - 芹菜工人的嘲笑给出了身份验证错误
- visual-studio-code - Vscode 转到定义不适用于 jinja 文件