0

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

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.