Push or update an object in array in array with redux

I would like to know how can I update a specific array or property in array with in array with redux.

Currently, I have my state :

State

props1 : "props1",
props2 : "props2",
props3 : "props3",
buckets : [{
    bucketProps1 : "bucketProps1",
    bucketProps2 : "bucketProps2",
    bucketProps3 : "bucketProps3",
    blocks : [{
        blockProps1 : "blockProps1",
        blockProps2 : "blockProps2",
        blockProps3 : "blockProps3",
        messages : [{
            messageProps1 : "messageProps1",
            messageProps2 : "messageProps2",
            messageProps3 : "messageProps3",
            replies : [{ 
                repliesProps1 : "repliesProps1",
                repliesProps2 : "repliesProps2",
                repliesProps3 : "repliesProps3",
            },
            {...
            }],
        },
        {...
        }],
    },
    {...
    }],
},
{...
}]

I would like to add a reply in the replies array :

reducer-sequences.js

case SEQUENCES.ADD_REPLY :

let newReplies = action.payload;

return {
...state,
buckets: state.buckets.map((bucket, i) => i === 0 ? {
        ...bucket,
        blocks: state.bucket.blocks.map((block, i) => i === 0 ? {
            ...block,
            messages: state.block.messages.map((message, i) => i === 0 ? {
                ...message,
                replies: [...state.buckets[0].blocks[0].messages[0].replies, newReplies]
            } : message)
        } : block)
    } : bucket)
};

But I have this error :

TypeError: Cannot read property 'blocks' of undefined

Upvotes: 0

Views: 57

Answers (2)

Nicolae Maties
Nicolae Maties

Reputation: 2655

I think that instead of doing:

blocks: state.blocks.map

You should do:

blocks: bucket.blocks

The same thing happens for the next map too: Change it to: block.messages

Upvotes: 1

Michaell94
Michaell94

Reputation: 514

You should make something like: {...replies} not ...replies.

But, try to refactor your code to avoid complicated solutions like that.

Upvotes: 0

Related Questions