Answer the question
In order to leave comments, you need to log in
Calculate state values React?
There is a state:
state = {
productsList: [ {
"name":"JACKET",
"price":"10",
"id":"4o"
},
{
"name":"JACKET",
"price":"9.9",
"id":"4"
},
{
"name":"SHOE",
"price":"101",
"id":"65"
},
{
"name":"Enduro",
"price":"15",
"id":"6jy5"
}]
};
countPrice = () => {
let sum = 0;
return this.state.productsList.map((product) => {
console.log(sum);
return sum += product.price;
})
}
Warning: Functions are not valid as a React child. This may happen if you return a Component instead of <Component /> from render. Or maybe you meant to call this function rather than return it.
Answer the question
In order to leave comments, you need to log in
const sum = this.state.productsList.reduce((sum, n) => sum + +n.price, 0);
{this.countPrice}
. And it is necessary {this.countPrice()}
. Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question