如何将新值推送到 Store 对象?
How to push new value to Store object?
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
}
如何将新值推送到 Store 对象?
这应该有效:
case 'FRUIT_ADD_SUCCESS':
action.payload = 'kiwi';
const newFoodList = {
...state.foodList,
fruits: [...state.foodList.fruits, action.payload],
};
return {
...state,
newFoodList,
};
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
}
如何将新值推送到 Store 对象?
这应该有效:
case 'FRUIT_ADD_SUCCESS':
action.payload = 'kiwi';
const newFoodList = {
...state.foodList,
fruits: [...state.foodList.fruits, action.payload],
};
return {
...state,
newFoodList,
};