如何将新值推送到 Store 对象?
Posted
技术标签:
【中文标题】如何将新值推送到 Store 对象?【英文标题】:How to push new value to Store object? 【发布时间】:2018-02-07 14:43:09 【问题描述】:foodList :
'fruits' : ['apple', 'banana'],
'hamburgers: ['bigmac', 'crispy']
在上述数据结构的情况下,如何在 Redux 中为水果添加新的kiwi
?
case "FRUIT_ADD_SUCCESS" :
action.payload = 'kiwi';
return
...state,
// Cannot read property 'push' of undefined
foodList: state.foodList['fruits'].push(action.payload)
// Disappear previous values
foodList: state.foodList['fruits'] = action.payload
【问题讨论】:
【参考方案1】:这应该可行:
case 'FRUIT_ADD_SUCCESS':
action.payload = 'kiwi';
const newFoodList =
...state.foodList,
fruits: [...state.foodList.fruits, action.payload],
;
return
...state,
newFoodList,
;
【讨论】:
以上是关于如何将新值推送到 Store 对象?的主要内容,如果未能解决你的问题,请参考以下文章