Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

105
Views
Why is my Grand Total not updating after I increase or decrease the quantity in React?

This is a basic application that I am working on using React and Bootstrap. Here, The Grand Total is not changing when I update the quantity. I have attached the Sandbox link. Please let me know the mistake.

https://codesandbox.io/s/reactbootstrap-forked-k9n86s?file=/src/App.js

  /********Total quantity *************** */
  const grandTotal = () => {
    let total = 0;
    for (let product of products) {
      total += product.price * product.quantity;
    }
    console.log(total);
    return total;
  };
<tr className="text-end">
    <td colSpan={3}>Grand Total:</td>
    <td>{grandTotal()}</td>
</tr>

Thanks.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

In grandTotal you still refer to the initial array (products). It should be product.

  const grandTotal = () => {
    let total = 0;
    for (let p of product) {
      total += p.price * p.quantity;
    }
    console.log(total);
    return total;
  };

NOTE: Be clear when naming things. I would name initial products as initialProductsand use products variable name in useState.

Edit reactBootstrap (forked)

about 4 years ago · Juan Pablo Isaza Report

0

the name of your variable is the problem products

const grandTotal = () => {
    let total = 0;
    for (let prd of product) {
      total += prd.price * prd.quantity;
    }
    console.log(total);
    return total;
  };

products point to this variable

const products = [
  {
    sno: "123",
    name: "Apple Watch",
    price: 290,
    quantity: 2
  },
  {
    sno: "567",
    name: "Samgung Watch",
    price: 278,
    quantity: 5
  },
  {
    sno: "785",
    name: "Cas",
    price: 123,
    quantity: 1
  }
];

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!