繁体   English   中英

使用 reducer 更新状态内的特定值

[英]Update an specific value inside the state with the reducer

我正在尝试更新状态内的对象数组的特定值(在本例中为列表内的注释) 在这种情况下, msgs键内的注释。

 const list = [{
    id: 1,
    text: "list text",
    msgs: [{
      id: 1,
      comment: "comment",
      commentID: "ab37afa-c17-e4f-f103-4715b72f14ec"
    }]
  },
  {
    id: 2,
    text: "list text 2",
    msgs: [{
      id: 2,
      comment: "comment2", <--- trying to update this value
      commentID: "ab37afa-c17-e4f-f103-4715b72f14ec-2"
    },
    {
      id: 2,
      comment: "comment3",
      commentID: "ab37afa-c11323127-e4f-f103-4715b72f14ec-2"
    }]
  }
];

我目前正在尝试使用减速器:

case "EDIT_COMMENT":
      temp = state.filter((list => list.id === parseInt(action.comment.id)))  //getting the list that I want
      const msgs = [].concat(...temp).map(x=> ({
        id: x.id,
        comment : x.comment,
        commentID : x.commentID
        })) // trying to get the messages inside the state


     // I honestly don't know what to put here
      return msgs.map( msg => {
        if(msg.commentID === action.payload.commentID){
          return {...}
        }
      }        
        )

我无法获得使用减速器更新我想要的数据的答案/解决方案,如果有人可以帮助我解决问题,我将不胜感激。 提前致谢,周末愉快:)

如果我理解正确,您想要更新任何给定state项上的任何嵌套msg评论(我假设它与您的问题中显示的列表形式匹配) - 在这种情况下,一个简单的解决方案可能如下:

case "EDIT_COMMENT": {

  return state.map(messageItem => {

    return {
      // Clone input messageItem
      ...messageItem,

      // Overwrite msgs field with new msgs array
      msgs : messageItem.msgs.map(commentItem => {

        if(commentItem.commentID === action.payload.commentID) {
          return {
            // Clone input commentItem
            ...commentItem,

            // Overwrite fields that this action should update,
            // on commentItem matching by comment id ie, "comment"
            // from action payload
            comment : action.payload.comment
          }      
        }
        else {
          // If message does not match actions comment id, return
          // a clone of commentItem to mapped list
          return {...commentItem}
        }
      })
    }    
  });
}

这里的假设是:

  • state是一个匹配list形式的list ,如您的问题所示
  • 您的操作负载中的commentIdmsg子数组中的评论是唯一的。
  • 您的操作负载中的comment字段包含新的评论字符串以更新现有的匹配评论

希望有帮助!

您想映射所有内容,按原样返回,除非它与action.payload.commentID匹配。 如果匹配,则按原样返回 THAT,但更新注释除外。

 const list = [{ id: 1, text: "list text", msgs: [{ id: 1, comment: "comment", commentID: "ab37afa-c17-e4f-f103-4715b72f14ec" }] }, { id: 2, text: "list text 2", msgs: [{ id: 2, comment: "comment2", // <--- trying to update this value commentID: "ab37afa-c17-e4f-f103-4715b72f14ec-2" }, { id: 2, comment: "comment3", commentID: "ab37afa-c11323127-e4f-f103-4715b72f14ec-2" }] } ]; const action = { payload: { commentID: 'ab37afa-c17-e4f-f103-4715b72f14ec-2', comment: 'Updated Comment', } } console.log(list.map(listItem => { return { ...listItem, msgs: listItem.msgs.map(msg => { if (msg.commentID === action.payload.commentID) { return { ...msg, comment: action.payload.comment } } return msg; }) } }))

这个怎么样? (我只是编造了动作的结构)

case "EDIT_COMMENT":
    return state.map((list) => {
        if (list.id === parseInt(action.listId) {
            list.msgs = list.msgs.map((msg) => {
                if (msg.id === action.commentId) {
                    msg.comment = action.comment;
                }
                return msg;
            }
        }
        return list;
    });
    break;

暂无
暂无

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

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