这是响应数据。当使用按递增和递减时,我需要存储每个项目的数量。
{name: "McChicken", type: "Non-Veg", price: 275, tax: 10}
{name: "Pizza", type: "Non-Veg", price: 275, tax: 10}
{name: "Burger", type: "Non-Veg", price: 275, tax: 10}
Redux 状态:在基于对象的存储中保存状态
因此,根据密钥很容易更新。
import * as types from '../actions/types';
var _ = require('lodash');
const INITIAL_STATE = {
data: {},
loading: false,
error: '',
cartData: {},
};
export default (state=INITIAL_STATE, action) => {
switch (action.type) {
case types.MENULIST_REQUEST_START:
return { ...state, loading:true, error: '' };
case types.MENULIST_SUCCESS:
const newItems = _.mapKeys(array, 'name')
return { ...state, data: newItems , loading:false, error: 'Success' };
case types.UPDATE_ITEM:
return { ...state, [cartData.name]: action.payload }
case types.DELETE_ITEM:
return _.omit(state, [cartData.name]: action.payload)
case types.MENULIST_FAILED:
return { ...state, data: {}, loading:false, error: 'Request failed' };
default:
return state;
}
}
调用更新和删除项目操作时出错。
错误:未定义购物车数据
如果响应更新,如何更新数量并管理状态?
你需要改变
{ ...state, cartData.name: action.payload }
es6 计算属性键
{ ...state, [cartData.name]: action.payload }
计算的属性键