-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
52 lines (46 loc) · 1.58 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import Counters from './components/counters';
import Nav from './components/nav';
class App extends React.Component{
constructor(props){
super(props)
this.state = {
counters:[
{ id: 1, value: 4 },
{ id: 2, value: 0 },
{ id: 3, value: 0 },
{ id: 4, value: 0 }
]
};
this.handleDelete = this.handleDelete.bind(this);
this.handleReset = this.handleReset.bind(this);
this.handleIncrement = this.handleIncrement.bind(this);
}
handleDelete(counterId){
const counters = this.state.counters.filter(data => data.id != counterId);
this.setState({ counters });
}
handleIncrement(counter){
const counters = [...this.state.counters];
const index = counters.indexOf(counter);
counters[index] = counter;
counters[index].value++;
this.setState({ counters });
}
handleReset(){
const counters = this.state.counters.map(counter => {
counter.value = 0;
return counter;
});
this.setState({ counters });
}
render(){
return (
<React.Fragment>
<Nav totalCounter={this.state.counters.filter(counter => { return counter.value > 0}).length } />
<Counters counters={this.state.counters } onIncrement={this.handleIncrement} onDelete={this.handleDelete} onReset={this.handleReset}></Counters>
</React.Fragment>
);
}
}
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(<App/>);