简体   繁体   English

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

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

I am using redux-saga and immutable.js in my react app. 我在我的React应用程序中使用redux-saga和immutable.js。 I have notifications array holds the data of every notification created by redux actions for whole app. 我有通知数组保存整个应用程序的redux操作创建的每个通知的数据。

My immutable redux store and notifications array is like this : 我不变的redux存储和通知数组是这样的:

global : 
  notifications: 
   0:
    key: 21339298 // random key to use like id
    show: true
    type: success
    message: Operation was successfull
   1: {key..., show...}
   ...

I want to find a single notification by "key" and update it's "show" value to false. 我想通过“键”查找单个通知,并将其“显示”值更新为false。

I read immutable.js documentation but it is very difficult to understand. 我阅读了immutable.js文档,但很难理解。 So i tried below code snippets. 所以我尝试了下面的代码片段。 But i didn't get a result. 但是我没有得到结果。

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);
        }
    );

How can i find an item and update its some value ? 我如何找到一个物品并更新其某些价值?

Solution is to seperate find index and update, first finding index and then using it with setIn . 解决方案是分离查找索引并进行更新,首先查找索引,然后将其与setIn一起setIn Below is the solution. 下面是解决方案。

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