最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Redux : update an item in an array of objects - Stack Overflow

programmeradmin1浏览0评论

In my reducer called 'reducer_product_list', I have this array :

let initialState = [
    {name: 'Article 1', price: 5, quantity: 10},
    {name: 'Article 2', price: 15, quantity: 8},
    {name: 'Article 3', price: 7, quantity: 15},
    {name: 'Article 4', price: 9, quantity: 5},
    {name: 'Article 5', price: 11, quantity: 100},
    {name: 'Article 6', price: 23, quantity: 20},
  ]

When I get the action 'ADD_TO_CART', I want to decrease the quantity of the selected object. The payload is one of those objects.

I typed the code above :

export default (state = initialState, action) => {
  switch (action.type) {
    case 'ADD_TO_CART':
      initialState.map((product, i) => {
        if (product.name === action.payload.name) {
          initialState[i].quantity -= 1
          return state;
        }
      });
    default: return state
  }
}

If I console.log my initialState, the quantity is decreasing, but in my container that renders the view, the quantity stays the same.

Thank you for your help.

In my reducer called 'reducer_product_list', I have this array :

let initialState = [
    {name: 'Article 1', price: 5, quantity: 10},
    {name: 'Article 2', price: 15, quantity: 8},
    {name: 'Article 3', price: 7, quantity: 15},
    {name: 'Article 4', price: 9, quantity: 5},
    {name: 'Article 5', price: 11, quantity: 100},
    {name: 'Article 6', price: 23, quantity: 20},
  ]

When I get the action 'ADD_TO_CART', I want to decrease the quantity of the selected object. The payload is one of those objects.

I typed the code above :

export default (state = initialState, action) => {
  switch (action.type) {
    case 'ADD_TO_CART':
      initialState.map((product, i) => {
        if (product.name === action.payload.name) {
          initialState[i].quantity -= 1
          return state;
        }
      });
    default: return state
  }
}

If I console.log my initialState, the quantity is decreasing, but in my container that renders the view, the quantity stays the same.

Thank you for your help.

Share Improve this question asked Sep 24, 2018 at 10:23 cyrilbcyrilb 1071 gold badge1 silver badge5 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 19

Try this:

export default (state = initialState, action) => {
  switch (action.type) {
    case 'ADD_TO_CART':
      return state.map(product => {
        if (product.name === action.payload.name) {
          return {...product, quantity: product.quantity-1}
        };
        return product;
      });
    default: return state
  }
}

The reason is you have to return a new state object derived from the current state reflecting the desired changes according to the requested action. See Redux Reducers for more details.

Thank you for you help, it works well! I just had to change one thing: instead of mapping on the initialState, I mapped on the state.

export default (state = initialState, action) => {
  switch (action.type) {
    case 'ADD_TO_CART':
      return state.map(product => {
        if (product.name === action.payload.name) {
          return {...product, quantity: product.quantity - 1}
        };
        console.log(state)
        return product;
      });
    default: return state
  }
}
发布评论

评论列表(0)

  1. 暂无评论