in counters component counters have 4 arrays of items I want to display their value on header(h1) when we click on increment button it gives 4, <h1>{this.state.counters.filter(c=>c.value>0).length}</h1>
I want to add all increment in h1 here is my code of parent component
class Counters extends Component {
state={
counters:[
{id:1, value:0},
{id:2, value:0},
{id:3, value:0},
{id:4, value:0}
],
}
handleIncrement=counter=>{
const counters =[...this.state.counters];
const index = counters.indexOf(counter);
counters[index]={...counter}
counters[index].value++;
this.setState({counters})
}
handleDelete=(counterid)=>{
const counters = this.state.counters.filter(m=>m.id !== counterid)
this.setState({counters})
}
handleReset=()=>{
const counters = this.state.counters.map(m=>{
m.value = 0;
return m
})
this.setState({counters})
}
render() {
return (
<div>
<h1>{this.state.counters.filter(c=>c.value>0).length}</h1>
<button onClick={this.handleReset} className="btn btn-secondary btn-sm">RESET</button>
{this.state.counters.map(m=>
<Counter key={m.id}
id={m.id} getDelete={this.handleDelete}
onIncrement={this.handleIncrement}
counter={m}
>
</Counter>) }
</div>
);
}
}