2

Here is my example initial state:

let initialState = {
    data: {
        name: 'john',
          books: [
              {
                  name: 'a',
                  price: 220
              }
          ]
    }
}

How I can add new book item to books ?

I tried already

return {
        ...state,
        data :{
          ...state.data,
          books:{
              ...state.data.books,
              name : state.data.books.concat(action.payload.item.name),
              price : state.data.books.concat(action.payload.item.price)
          }
      }
      };

Any idea for that?

Important: I don't want to delete item which already in books I want to add new one with action payload

2
  • Why switching books from array to object ? Commented Aug 22, 2019 at 16:58
  • do you wanted name and price to be name: 'aaa, aaa' like this?? Commented Aug 22, 2019 at 17:04

4 Answers 4

6

You missed some braces there, especially, books is array and you are inserting new object

return {
        ...state,
        data: {
          ...state.data,
          books: [
              ...state.data.books,
              {
                  name: action.payload.item.name,
                  price: action.payload.item.price
              }
          ]
      }
};
Sign up to request clarification or add additional context in comments.

2 Comments

In this case name and price will be arrays, but they're strings.
I edited it, I think this author was trying to accomplish
0

First of all, books is array. Second, if you want just to add a new item to the array, you can use array spread operator.

books: state.data.books.concat(action.payload.item) would also work.

return {
  ...state,
  data: {
    ...state.data,
    books: [
      ...state.data.books,
      action.payload.item
    ]
  }
};

Comments

0

Let's make our jobs easier by inventing tools of convenience -

const update = (key, { [key]: value, ...state }, f) =>
  ({ ...state, [key]: f (value) })

const initState =
  { data:
      { name: "john"
      , books: [ { name: "a", price: 20 } ]
      }
  }

const payload =
  { item: { name: "b", price: 30 } }

const nextState =
  update ('data', initState, x =>
    update ('books', x, arr =>
      [ ...arr, payload.item ]
    )
  )

console.log(initState)
console.log(nextState)

Output

// initState
{ data:
    { name: "john"
    , books:
        [ { name: "a", price: 20 } ]
    }
}

// nextState
{ data:
    { name: "john"
    , books:
        [ { name: "a", price: 20 }
        , { name: "b", price: 30 }
        ]
    }
}

Comments

-1

The answer is easy look at this

const book = {
            name : action.payload.item.name,
            price : action.payload.item.price
           };
return {
    ...state,
    data :{
      ...state.data,
      books: state.data.books.concat(book)              
      }      
};

you can find immutable update patterns here

Redux updated patterns

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.