0

This is my initial state constant object, I'm trying to add a new comment items into comments but this code which is not pushing my object into it where i'm doing wrong thanks in advance.

    export const comments = [];

    export const BlogPostReducer = (state = comments, action) => {
        switch (action.type) {
            case 'ADD_COMMENT':
                return [
                ...state,
                {
                    name: action.comment.name,
                    subject: action.comment.subject,
                    message: action.comment.message
                }
            ];
            default:
                return state;
        }
    };
after i used see console here...still im getting empty state 

image here

1
  • @Hana Alaydrus can you help me on this Commented Jul 19, 2017 at 4:42

4 Answers 4

1

For pushing new object you need to do as

return [
    ...state,
    {
       name: action.comment.name,
       subject: action.comment.subject,
       message: action.comment.message
    }
];

This will create and new array, push the object in it and return it

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

1 Comment

hi can you check the console image i have attached to my question after usage of your code
0

If you want to add it to an array in the immutable way, you should use 'concat'

try below code.

export const comments = [];

export const BlogPostReducer = (state = comments, action) => {
    switch (action.type) {
        case 'ADD_COMMENT':
            return state.concat({name: action.comment.name,subject: action.comment.subject,message: action.comment.message});
        default:
            return state;
    }
};

Comments

0

This way i have achieved,

const addCommentToArray = (state, action) => {
    return [...state.comments, {
        name: action.comment.name,
        subject: action.comment.subject,
        message: action.comment.message
    }];
}

export const BlogPostReducer = (state = initialState, action) => {
    switch (action.type) {
        case 'ADD_COMMENT':
            return Object.assign({}, state, { comments: addCommentToArray(state, action) });

        default:
            return state;
    }
};

Comments

0

You need to pass comment to store as argument and append to comments.

export const BlogPostReducer = (state, action) => {
    switch (action.type) {
        case 'ADD_COMMENT':
            let { comments } = state;
            comments = comments || [];
            comments.push(action.comment);
            state.comments = comments;
            return state;
        default:
            return state;
    }
};

1 Comment

This is a very bad approach as it mutates the state. @kumar's answer is much better.

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.