小编Con*_*ght的帖子

Redux 业务逻辑最佳实践

我使用 React 和 Redux 构建了一个购物车,但无法理解最佳实践流程。

我的购物车操作:

export const addToCart = (product) => (dispatch, getState) => {
  let { products: cartProducts } = getState().cart;
  let { newCartProducts, totalPrice } = handleAddToCart(cartProducts, product);
  dispatch(
    add({
      products: newCartProducts,
      total: totalPrice,
    })
  );
};

Run Code Online (Sandbox Code Playgroud)

模拟服务器处理程序:(更新产品的所有逻辑都在这里 => 我的主要问题是这是否有意义。

export function handleAddToCart(cartProducts, currentProduct) {
  let idx = cartProducts.findIndex((p) => p.id === currentProduct.id);
  let productInCart = cartProducts[idx];
  if (productInCart) {
    let updatedProduct = {
      ...currentProduct,
      quantity: productInCart.quantity + 1,
      price:
        productInCart.price +
        applySale({
          ...currentProduct,
          quantity: productInCart.quantity + …
Run Code Online (Sandbox Code Playgroud)

logic frontend flux reactjs redux

1
推荐指数
1
解决办法
1017
查看次数

标签 统计

flux ×1

frontend ×1

logic ×1

reactjs ×1

redux ×1