score:2

Accepted answer

assuming create_post refers to add_post based on receiving post data, it would look like this :

case create_post:
  return {...state, items: state.items.concat(action.post)};

no need to use object assign since you can just concat your post with the already existing posts from your state. as a reminder, concat does not mutate state, it returns a new array with the added post.

score:1

this is done assuming that a single post inside an array is being passed

function posts(
    state = {
        items: []
    },
    action
) {
    switch (action.type) {
        case receive_posts:
            return object.assign({}, state, {
                items: action.posts,
            })
        case create_post:
            return object.assign({}, state, {
                items: [...state.items, ...action.post]
            })
        default:
            return state
    }
}

Related Query

More Query from same tag