Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 为处于Redux状态的对象中的每个项更新单个值_Reactjs_Redux_React Redux - Fatal编程技术网

Reactjs 为处于Redux状态的对象中的每个项更新单个值

Reactjs 为处于Redux状态的对象中的每个项更新单个值,reactjs,redux,react-redux,Reactjs,Redux,React Redux,我在redux状态中有一个通知列表。正如你在下面看到的 列表中可能有更多项目,我的目标是根据isDiscussionType值,将列表中所有项目的isRead值设置为true。我在减速器中使用以下代码: case MARKALLASREAD_NOTIFICATIONS_SUCCESS: return { ...state, loading: false, notifications: Object.keys(stat

我在redux状态中有一个通知列表。正如你在下面看到的

列表中可能有更多项目,我的目标是根据
isDiscussionType
值,将列表中所有项目的
isRead
值设置为
true
。我在减速器中使用以下代码:

case MARKALLASREAD_NOTIFICATIONS_SUCCESS:
    return {
        ...state,
        loading: false,
        notifications:
            Object.keys(state.notifications).map(id => {
                if (state.notifications[id].isDiscussionType == action.payload.isDiscussionType)
                    return { ...state.notifications[id], isRead: true }
                else
                    return { ...state.notifications[id] }
            })

    };
此代码导致以下状态,其中
丢失(设置为0),并且
isRead
值未更改(尽管数据库已正确更新)

你看到我上面分享的代码中有什么问题吗


如果您的
通知是一个列表,即数组,则不应在其上使用
对象。键
,直接使用
映射

case MARKALLASREAD_NOTIFICATIONS_SUCCESS:
  return {
    ...state,
    loading: false,
    notifications: state.notifications.map(notification => {
      if (notification.isDiscussionType === action.payload.isDiscussionType) {
        return {
          ...notification,
          isRead: true
        }
      } else {
        return notification;
      }
    })
  };

如果您的
通知
是一个列表,即数组,则不应在其上使用
对象键
,而应直接使用
映射

case MARKALLASREAD_NOTIFICATIONS_SUCCESS:
  return {
    ...state,
    loading: false,
    notifications: state.notifications.map(notification => {
      if (notification.isDiscussionType === action.payload.isDiscussionType) {
        return {
          ...notification,
          isRead: true
        }
      } else {
        return notification;
      }
    })
  };

Map返回数组而不是对象。这就是您丢失id的原因,因为0只是数组中的索引,该数组现在处于通知状态。我会将通知的生成从返回中移出,以获得更大的灵活性:

case MARKALLASREAD_NOTIFICATIONS_SUCCESS:
    const notifications = { ...state.notifications }
    Object.values(notifications).forEach(notification => {
        if(notification.isDiscussionType === action.payload.isDiscussionType) {
           notifications[notification.id] { ...notification, isRead: true }
        }
    }
    return {
        ...state,
        loading: false,
        notifications
    };
如果isDiscussionType相同,则每次通知更改时,都将返回一个不可变对象。因为map、filter、reduce返回数组,所以我会将它移出返回并使用forEach


希望这有帮助。愉快的编码。

Map返回一个数组而不是一个对象。这就是您丢失id的原因,因为0只是数组中的索引,该数组现在处于通知状态。我会将通知的生成从返回中移出,以获得更大的灵活性:

case MARKALLASREAD_NOTIFICATIONS_SUCCESS:
    const notifications = { ...state.notifications }
    Object.values(notifications).forEach(notification => {
        if(notification.isDiscussionType === action.payload.isDiscussionType) {
           notifications[notification.id] { ...notification, isRead: true }
        }
    }
    return {
        ...state,
        loading: false,
        notifications
    };
如果isDiscussionType相同,则每次通知更改时,都将返回一个不可变对象。因为map、filter、reduce返回数组,所以我会将它移出返回并使用forEach


希望这有帮助。愉快的编码。

我认为它是一个对象,因为如果不是id,为什么它会以1开头呢?嗯,很好。在这种情况下,OP将一个对象更改为数组。是的,这就是问题所在,但我还不知道如何有效地更新这些对象。如果它只是一个id,这将很容易,但它是嵌套的。谢谢!那我怎么能把它作为一个对象呢?看我上面的答案,我认为它是一个对象,因为如果不是id,为什么它会以1开头呢?嗯,很好。在这种情况下,OP将一个对象更改为数组。是的,这就是问题所在,但我还不知道如何有效地更新这些对象。如果它只是一个id,这将很容易,但它是嵌套的。谢谢!那么我怎样才能把它作为一个对象呢?看我上面的答案