计算总数不返回任何React



我正在使用React构建一个基本的商店应用程序。这里有一个计算产品最终价格的函数。代码:

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 });
};

国家有:

total: 0,

这里它应该返回所有产品的值:

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

但是,即使添加了项,h1中也显示为0。

我在calculateTotal函数中尝试的另一件事是-

return total2;

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

还是不行。如何修复?由于

我想这就是第一条评论的意思。

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>`

相关内容

  • 没有找到相关文章

最新更新