-
Notifications
You must be signed in to change notification settings - Fork 0
/
changecolor.jsx
43 lines (41 loc) · 1.38 KB
/
changecolor.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
class App extends React.Component {
constructor (props) {
super(props);
// binding method agar mendapat context `this` class
this.handleChangeColorClick = this.handleChangeColorClick.bind(this);
this.state = {
colorFirst: 'orange',
colorLast: 'red'
};
}
handleChangeColorClick(event) {
if (this.state.colorFirst === 'orange') {
this.setState({
colorFirst: 'red',
colorLast: 'orange'
});
return;
}
this.setState({
colorFirst: 'orange',
colorLast: 'red'
});
}
render() {
return (
<React.Fragment>
<div className={'color color-' + this.state.colorFirst}>
<button type="button" className="button" onClick={this.handleChangeColorClick}>
Change Color ({this.state.colorLast})
</button>
</div>
<div className={'color color-' + this.state.colorLast}>
<button type="button" className="button" onClick={this.handleChangeColorClick}>
Change Color ({this.state.colorFirst})
</button>
</div>
</React.Fragment>
);
}
}
ReactDOM.render(<App />, document.getElementById('container'));