如何在化简器中推入嵌套数组

问题描述 投票:0回答:1

当前正在尝试将输入字段值等推入replyComments中,但是即使在分派此操作时它仍然为空。...reducer首先检查majorIndex,然后是minorIndex,从那里应该将其推入replyComments。谁知道我该如何解决?

 case types.REPLY_COMMENT: {
            return {
                ...state,
                enableToggleComment: true,
                imageData: state.imageData.map(image => image.id === action.majorIndex ?
                    {   
                        ...image, 
                        comments: {
                            [action.minorIndex]: {
                                replyComments: [...image.comments.replyComments, { comment: action.newComment, likeComment: image.toggle, enableReply: false }]
                            }
                        }
                        } : image
                    )
            }
        }
javascript reactjs reducers
1个回答
0
投票

使用

...
comments: {
    [action.minorIndex]: {
        replyComments: replyComments.concat([{ comment: action.newComment, likeComment: image.toggle, enableReply: false }])
    }
}
...

可能有效,我猜可能是由于突变问题引起的

© www.soinside.com 2019 - 2024. All rights reserved.