-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcounter-complete.js
49 lines (37 loc) · 1.27 KB
/
counter-complete.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
import React, { useState } from 'react';
import ReactDOM, { render } from 'react-dom';
class Counter extends React.Component {
constructor(props) {
super(props);
//add "counter" variable to state
this.state = {
count: 0
}
//bind the handle click method to the class
this.handleClick = this.handleClick.bind(this);
}
//create a handleClick method
handleClick() {
this.setState(state => (
{count: state.count+=1}
));
}
//add an onClick method to the increase button that will increase the "0" based on changes in state
render() {
return (
<div id="mainArea">
<p>Button count: <span>{this.state.count}</span></p>
<button id="mainButton" onClick={this.handleclick}>Increase</button>
</div>
)
}
}
ReactDOM.render(
<Counter />,
document.getElementById('root')
)
//use this.state to add a counter to state
//create a handleClick method in the constructor that, when invoked, increases the counter in state by one. (Hint, use this.setState to alter state)
//use the bind method to bind the handle click method to the Counter class
//inside the render/return, add onClick functionality to the button; invoke handleClock on each click.
//replace the "0" with a correct reference to the counter variable in state.