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

167
Views
Calculating total not returning anything React

I'm building a basic shop app using React. So here I have this function that calculates the final price of the products. Code:

    calculateTotal = () => {
    const total2 =
      this.state.final_apple_price +
      this.state.final_avocado_price +
      this.state.final_branzino_price +
      this.state.final_bread_price +
      this.state.final_burger_price +
      this.state.final_carrot_price +
      this.state.final_cheese_price +
      this.state.final_chicken_price +
      this.state.final_donut_price +
      this.state.final_egg_price +
      this.state.final_falooda_price +
      this.state.final_milk_price +
      this.state.final_muffin_price +
      this.state.final_orange_price +
      this.state.final_pizza_price +
      this.state.final_potato_price +
      this.state.final_salmon_price +
      this.state.final_strawberry_price;
    this.setState({ total: total2 });
  };

The state has:

total: 0,

And here it's supposed to return the values of all the products:

<h1>{this.state.total}</h1>

However, 0 is displayed in the h1, even when items are added.

Another thing which I tried in the calculateTotal function is -

return total2;

and

<h1>{this.calculateTotal()}</h1>

Still, it isn't working. How to fix? Thanks

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

0

I think this is what the first comment meant.

calculateTotal = () => {
    return this.state.final_apple_price +
      this.state.final_avocado_price +
      this.state.final_branzino_price +
      this.state.final_bread_price +
      this.state.final_burger_price +
      this.state.final_carrot_price +
      this.state.final_cheese_price +
      this.state.final_chicken_price +
      this.state.final_donut_price +
      this.state.final_egg_price +
      this.state.final_falooda_price +
      this.state.final_milk_price +
      this.state.final_muffin_price +
      this.state.final_orange_price +
      this.state.final_pizza_price +
      this.state.final_potato_price +
      this.state.final_salmon_price +
      this.state.final_strawberry_price;
  };

  // ...
  // inside render
  `<h3>{this.calculateTotal()}</h3>`
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!