You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Immer实现购物车商品增减的代码是否合规?是否存在遗漏要点?

Is Your Immer Cart Code Following Best Practices? Let's Check!

Hey there! Let's dive into your Immer-based shopping cart code and break down whether it's following Immer's norms, plus cover any key considerations you might want to keep in mind.

First: Your Code Is Valid!

To start with good news: your current implementation is completely compliant with Immer's rules. Immer supports two styles of updating state in its produce context (which Redux Toolkit automatically wraps your slice reducers in):

  1. Returning a new state value (like you're doing with state.shopcart = [...state.shopcart, action.payload] and filter)
  2. Writing "mutative" code that looks like you're directly modifying the state (Immer converts this under the hood to immutable updates)

So your code will work perfectly fine—no issues with correctness here.

Optimizing for Immer's Design Philosophy

While your code is valid, you can lean into Immer's core strength: making immutable updates feel like mutable ones. This makes your code more concise and readable. Here's how you could adjust your reducers to match this style:

addProduct: (state, action: PayloadAction<Omit<IProductsListDataOnPress, 'onPress' | 'onLongPress'>>) => {
  // Instead of spreading to create a new array, use push directly
  state.shopcart.push(action.payload);
},
removeProduct: (state, action: PayloadAction<{ id: string; }>) => {
  // Find the index and splice it out (Immer handles the immutable update)
  const productIndex = state.shopcart.findIndex(el => el.id === action.payload.id);
  if (productIndex !== -1) {
    state.shopcart.splice(productIndex, 1);
  }
}

Key Immer Notes You Might Want to Keep in Mind

Even if your current code is solid, these tips will help you avoid pitfalls and use Immer more effectively:

  • Don't mix update styles: Never combine returning a new state object (e.g., return { ...state, shopcart: newArray }) with mutating the state (e.g., state.shopcart.push(...)) in the same reducer. Immer will prioritize the returned value, and your mutations will be ignored.
  • Handle nested state properly: If your product objects have nested properties (like a quantity field you want to increment), you can modify them directly without spreading the entire object. For example:
    // If you had an incrementQuantity reducer
    incrementQuantity: (state, action: PayloadAction<{ id: string }>) => {
      const product = state.shopcart.find(el => el.id === action.payload.id);
      if (product) {
        product.quantity += 1; // Immer handles the immutable update here
      }
    }
    
  • Ensure initial state is properly set: Make sure state.shopcart is initialized as an empty array [] (not undefined). This prevents errors when calling array methods like push or filter.
  • Avoid unnecessary state replacements: If you only need to update a specific part of the state (like shopcart), don't replace the entire state object. Directly modifying the nested property is more efficient, as Immer only tracks changes to the parts you touch.
  • Be cautious with non-primitive values: If you're adding objects to the cart, ensure they're not references to mutable objects outside the reducer (though this is a general Redux best practice, not just Immer-specific).

内容的提问来源于stack exchange,提问作者universe11

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 22:07:35