Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,46 @@ class App extends Component {
{ id: 2, value: 0 },
{ id: 3, value: 0 },
{ id: 4, value: 0 }
]
],
total: 0
};

handleIncrement = counter => {
const counters = [...this.state.counters];
const index = counters.indexOf(counter);
counters[index] = { ...counters[index] };
counters[index].value++;
this.setState({ counters });
let total = this.state.total;
total++;
this.setState({ counters, total });
};

handleDecrement = counter => {
const counters = [...this.state.counters];
const index = counters.indexOf(counter);
counters[index] = { ...counters[index] };
counters[index].value--;
this.setState({ counters });
let total = this.state.total;
total--;
this.setState({ counters, total });
};

handleReset = () => {
const counters = this.state.counters.map(c => {
c.value = 0;
return c;
});
this.setState({ counters });
let total = 0;
this.setState({ counters, total });
};

handleDelete = counterId => {
const counters = this.state.counters.filter(c => c.id !== counterId);
this.setState({ counters });
let total = 0;
counters.forEach(c => {
total = total + c.value;
})
Comment on lines +47 to +50
Copy link

@bikkimahato bikkimahato Aug 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use reduce method also.
const total = counters.reduce((acc,el)=>acc+el.value,0);

this.setState({ counters, total });
};

handleRestart = () => {
Expand All @@ -54,6 +64,7 @@ class App extends Component {
<main className="container">
<Counters
counters={this.state.counters}
total={this.state.total}
onReset={this.handleReset}
onIncrement={this.handleIncrement}
onDecrement={this.handleDecrement}
Expand Down
5 changes: 5 additions & 0 deletions src/components/counters.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { Component } from "react";
import Counter from "./counter";
import Total from "./total";

class Counters extends Component {
render() {
Expand All @@ -9,6 +10,7 @@ class Counters extends Component {
onDelete,
onDecrement,
counters,
total,
onRestart
} = this.props;
return (
Expand Down Expand Up @@ -36,6 +38,9 @@ class Counters extends Component {
onDelete={onDelete}
/>
))}
<Total
value={total}
/>
</div>
);
}
Expand Down
18 changes: 18 additions & 0 deletions src/components/total.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React from 'react';

const Total = ({value}) => (
<div className="row">
<div className="col-md-1">
<span style={{ fontSize: 24 }} className="badge m-2 badge-">
Total
</span>
</div>
<div className="col-md-1">
<span style={{ fontSize: 24 }} className="badge m-2 badge-">
{value}
</span>
</div>
</div>
);

export default Total;