如何计算React Redux中的所有addTocart产品总价

发布于 2025-02-10 17:35:12 字数 1984 浏览 1 评论 0原文

<div className='container py-4 my-5'>
  {item.length === 0 ? (
    <p>
      cart is empty, <NavLink to='/'>continoue shopping</NavLink>
    </p>
  ) : (
    item.map((cart) => {
      return (
        <div className='row py-5 border-bottom'>
          <>
            <div className='col-md-4'>
              <img
                src={cart.image}
                alt=''
                style={{ width: '100%', height: '300px', objectFit: 'contain' }}
              />
            </div>
            <div className='col-md-4'>
              <h3> {cart.title} </h3>
              <p className='lead'> {cart.description} </p>
              <p>Quantity: {cart.qty}</p>
              <strong className='lead fw-bold'>
                {' '}
                price: {cart.price * cart.qty}{' '}
              </strong>

              <div className='mt-4'>
                <button
                  className='btn btn-outline-dark me-4'
                  onClick={() => Decrement(cart)}
                >
                  <i className='fa fa-minus'></i>
                </button>
                <button
                  className='btn btn-outline-dark'
                  onClick={() => addIncrement(cart)}
                >
                  <i className='fa fa-plus'></i>
                </button>
              </div>
            </div>
          </>
        </div>
      );
    })
  )}

  <div className='checkout'>
    <h2 className='my-4'>total price: </h2>
    <button className='btn btn-outline-dark px-5 py-2 green'>Checkout</button>
  </div>
</div>;

我正在使用简单的反应显示在购物车组件中的产品,我只是让如何计算所有购物车产品的总价格感到困惑。它是一个基于REDUX的Application,我正在从“通过MAP功能中的CART组件中使用Eletrector”获取数据。是否有可能仅计算所有我只是添加的所有购物车的总价格。”

<div className='container py-4 my-5'>
  {item.length === 0 ? (
    <p>
      cart is empty, <NavLink to='/'>continoue shopping</NavLink>
    </p>
  ) : (
    item.map((cart) => {
      return (
        <div className='row py-5 border-bottom'>
          <>
            <div className='col-md-4'>
              <img
                src={cart.image}
                alt=''
                style={{ width: '100%', height: '300px', objectFit: 'contain' }}
              />
            </div>
            <div className='col-md-4'>
              <h3> {cart.title} </h3>
              <p className='lead'> {cart.description} </p>
              <p>Quantity: {cart.qty}</p>
              <strong className='lead fw-bold'>
                {' '}
                price: {cart.price * cart.qty}{' '}
              </strong>

              <div className='mt-4'>
                <button
                  className='btn btn-outline-dark me-4'
                  onClick={() => Decrement(cart)}
                >
                  <i className='fa fa-minus'></i>
                </button>
                <button
                  className='btn btn-outline-dark'
                  onClick={() => addIncrement(cart)}
                >
                  <i className='fa fa-plus'></i>
                </button>
              </div>
            </div>
          </>
        </div>
      );
    })
  )}

  <div className='checkout'>
    <h2 className='my-4'>total price: </h2>
    <button className='btn btn-outline-dark px-5 py-2 green'>Checkout</button>
  </div>
</div>;

I'm showing products in cart component using simple react, im just confused how to calculate total price of all cart product. its an react redux based application, I'm getting data from "useSelector in cart component through map function. is there any possibility to just calculate total price of all cart which i just add."

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

标点 2025-02-17 17:35:12

您可以使用降低函数来计算总价:

const [total, setTotal] = useState(0.0);

useEffect(() => {
    // Update total when item qty changes
    const newTotal = item.reduce((a,b) => (a.qty * a.price) + (b.qty * b.price));
    setTotal(newTotal)
}, [item]) 

...

<div className='checkout'>
    <h2 className='my-4'>total price: {total}</h2>
    <button className='btn btn-outline-dark px-5 py-2 green'>Checkout</button>
</div>

You can use the reduce function to calculate the total price:

const [total, setTotal] = useState(0.0);

useEffect(() => {
    // Update total when item qty changes
    const newTotal = item.reduce((a,b) => (a.qty * a.price) + (b.qty * b.price));
    setTotal(newTotal)
}, [item]) 

...

<div className='checkout'>
    <h2 className='my-4'>total price: {total}</h2>
    <button className='btn btn-outline-dark px-5 py-2 green'>Checkout</button>
</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文