-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathButton.jsx
69 lines (58 loc) · 1.84 KB
/
Button.jsx
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import React from 'react';
import ReactDOM from 'react-dom';
class Button extends React.Component{
//old example for component counter prog
constructor(props){
super(props);
//this.state = {counter : 9}
this.handleClick=this.handleClick.bind(this);
}
handleClick () {
//first way
// this.setState({
// counter : this.state.counter + 1
// })
//second way
// this.setState((prevState)=>({
// counter: prevState.counter + 1
// }));
this.props.onClickFunction(this.props.incrementValue);
}
render(){
return(
<div>
<button onClick={this.handleClick}> +{this.props.incrementValue} </button>
{/* <button onClick={this.props.onClickFunction}> +1 </button> */}
</div>
);
};
}
const Result = (props) =>{
return(
<div> Result = {props.counter} </div>
);
};
class Reuse extends React.Component{
constructor(props){
super(props);
this.state = {counter : 0}
this.incrementCounter=this.incrementCounter.bind(this);
}
incrementCounter(incrementValue){
this.setState((prevState)=>({
counter: prevState.counter + incrementValue
}));
}
render(){
return(
<div>
<Button incrementValue={1} onClickFunction={this.incrementCounter}/>
<Button incrementValue={5} onClickFunction={this.incrementCounter}/>
<Button incrementValue={10} onClickFunction={this.incrementCounter}/>
<Button incrementValue={100} onClickFunction={this.incrementCounter}/>
<Result counter={this.state.counter}/>
</div>
);
}
}
export default Reuse;