【发布时间】:2022-12-14 03:27:32
【问题描述】:
I am using API of a fake store, so when I add a product to my cart I need to update the quantity of this product in the cart and update the quantity on UI. The problem is that product quantity is updating in global state, but not updating on UI. Could you please give any advice on that? Thank you in advance !
const reducer = (state= initialState, action) => { 开关(动作类型){ 案例“ADD_TO_CART”: const newItem = action.payload const exist = state.products.find((item) => item.id === newItem.id); 如果(存在){ 存在.数量+=1 返回 { ...状态, 数量:state.quantity + 1 } } 别的 { 新物品.数量 = 1 返回{ ...状态, 产品:state.products.concat(newItem) } }
【问题讨论】:
-
请分享价值
initialState -
const initialState ={ 产品: [], }
标签: react-redux