我在我的 React 应用程序中使用 redux-saga 和 immutable.js。 我有通知数组保存整个应用程序的 redux 操作创建的每个通知的数据。
我的不可变 redux 存储和通知数组如下所示:
global :
notifications:
0:
key: 21339298 // random key to use like id
show: true
type: success
message: Operation was successfull
1: {key..., show...}
...
我想通过“key”查找单个通知并将其“show”值更新为 false。
我阅读了 immutable.js 文档,但它很难理解。 所以我尝试了下面的代码片段。但我没有得到结果。
return state
.updateIn(['notifications',
ns => ns.findIndex(function (item) {
return item.get("key") === action.notificationId;}
), 'show'], false
);
return state
.update(
list.findIndex(function (item) {
return item.get("key") === action.notificationId;
}), function (item) {
return item.set("show", false);
}
);
我如何找到一个项目并更新它的一些值?
最佳答案
解决方案是将查找索引和更新分开,先查找索引,然后将其与setIn
一起使用。下面是解决方案。
case NOTIFY_HIDE:
let notifications = state.get('notifications');
let notificationIdx = notifications.findIndex(function (item) {
return item.get('key') === action.notificationId;
});
return state
.setIn(['notifications', notificationIdx, 'show'], false);
关于javascript - Immutable js - Redux-Saga 按键查找项目并更新值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54615745/