Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

170
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda