繁体   English   中英

不可变的js-Redux-Saga通过键查找项目并更新值

[英]Immutable js - Redux-Saga find item by key and update value

我在我的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...}
   ...

我想通过“键”查找单个通知,并将其“显示”值更新为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一起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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM