【问题标题】:How can I get total price of all product in cart in Reactjs如何在 Reactjs 中获取购物车中所有产品的总价
【发布时间】:2020-09-17 03:35:25
【问题描述】:

我有这样的代码

const Cart = ({ fetchCartRequest, productsInCart }) => {
  const [show, setShow] = useState(false);

  const handleClose = () => setShow(false);
  const handleShow = () => setShow(true);

  useEffect(() => {
    const userId = localStorage.getItem("userData");
    fetchCartRequest(userId);
  }, [fetchCartRequest]);


  return (
    <>
      <Image src={CartIcon} alt="Cart icon" onClick={handleShow} />
      <Modal show={show} onHide={handleClose} size="md">
        <Modal.Header closeButton>
          <Modal.Title>Cart detail</Modal.Title>
        </Modal.Header>
        <Modal.Body>
          {!!productsInCart && productsInCart.length > 0 ? (
            productsInCart.map((item, index) => {
              return (
                <CartContainer
                  key={index}
                  image={item.image}
                  name={item.name}
                  price={parseInt(item.price) * item.quantity}
                  quantity={item.quantity}
                />
              );
            })
          ) : (
              <h4 className="center-title">Cart list is empty!</h4>
            )}
          <div>
            <h3>Total price: </h3>
          </div>
        </Modal.Body>
        <Modal.Footer className="d-flex justify-content-center">
          <Button btnText="Checkout" onClick={handleClose} />
        </Modal.Footer>
      </Modal>
    </>
  );
};

export default Cart;

在上面的代码中,我已经获得了每件商品的总价,但我还没有获得购物车中所有商品的总价。我想知道,如何将购物车中产品的所有价格汇总并在 UI 中显示

【问题讨论】:

    标签: reactjs shopping-cart


    【解决方案1】:

    简单求和所有项目在js中使用数组reduce

    const Cart = ({ fetchCartRequest, productsInCart }) => {
        const [show, setShow] = useState(false);
    
        const handleClose = () => setShow(false);
        const handleShow = () => setShow(true);
    
        useEffect(() => {
            const userId = localStorage.getItem("userData");
            fetchCartRequest(userId);
        }, [fetchCartRequest]);
    
    
        return (
            <>
                <Image src={CartIcon} alt="Cart icon" onClick={handleShow} />
                <Modal show={show} onHide={handleClose} size="md">
                    <Modal.Header closeButton>
                        <Modal.Title>Cart detail</Modal.Title>
                    </Modal.Header>
                    <Modal.Body>
                        {!!productsInCart && productsInCart.length > 0 ? (
                            productsInCart.map((item, index) => {
                                return (
                                    <CartContainer
                                        key={index}
                                        image={item.image}
                                        name={item.name}
                                        price={parseInt(item.price) * item.quantity}
                                        quantity={item.quantity}
                                    />
                                );
                            })
                        ) : (
                            <h4 className="center-title">Cart list is empty!</h4>
                        )}
                        <div>
                            <h3>Total price:
                                {!!productsInCart && productsInCart.length > 0 ? (
                                    productsInCart.reduce((sum, item) => sum + parseInt(item.price) * item.quantity, 0)
                                ) : null}
                            </h3>
                        </div>
                    </Modal.Body>
                    <Modal.Footer className="d-flex justify-content-center">
                        <Button btnText="Checkout" onClick={handleClose} />
                    </Modal.Footer>
                </Modal>
            </>
        );
    };
    

    【讨论】:

      【解决方案2】:

      鉴于您已经在每行都这样做了,这似乎很明显,但这里是:

      productsInCart.reduce((sum, item) => sum + parseInt(item.price) * item.quantity, 0)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-12-09
        • 1970-01-01
        • 2019-07-19
        • 1970-01-01
        • 1970-01-01
        • 2012-12-05
        • 2021-08-10
        • 2017-04-27
        相关资源
        最近更新 更多