-
Notifications
You must be signed in to change notification settings - Fork 2
/
hello-react.html
112 lines (92 loc) · 3.78 KB
/
hello-react.html
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello React</title>
<script src="https://fb.me/react-with-addons-0.14.0.js"></script>
<script src="https://fb.me/react-dom-0.14.0.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/6.1.19/browser.min.js"></script>
<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
</head>
<body>
<div id="app"></div>
<script type="text/babel">
const GlyphIcon = React.createClass({
render: function() {
return(
<span className={'glyphicon glyphicon-' + this.props.icon}></span>
)
}
})
const Button = React.createClass({
render: function() {
return (
<button onClick={this.props.onClick}>{this.props.children}</button>
)
}
})
const HelloReact = React.createClass({
getDefaultProps: function() {
return {
likes: 0
}
},
getInitialState: function() {
return {
isIncreasing: false
}
},
componentWillReceiveProps: function(nextProps) {
this._logPropsAndState('componentWillReceiveProps()')
console.log('nextProps.likes: ' + nextProps.likes)
this.setState({isIncreasing: nextProps.likes > this.props.likes})
},
shouldComponentUpdate: function(nextProps, nextState) {
this._logPropsAndState('shouldComponentUpdate()')
console.log('nextProps.likes: ' + nextProps.likes
+ ' nextState.isIncreasing: ' + nextState.isIncreasing)
return nextProps.likes > 1;
},
componentDidUpdate: function(prevProps, prevState) {
this._logPropsAndState('componentDidUpdate')
console.log('prevProps.likes: ' + prevProps.likes
+ ' prevState.isIncreasing:' + prevState.isIncreasing)
console.log('componentDidUpdate() gives an opportunity to execute code after react is finished updating the DOM.')
},
_logPropsAndState: function(callingFunction) {
console.log('=> ' + callingFunction)
console.log('this.props.likes: ' + this.props.likes)
console.log('this.state.isIncreasing: ' + this.state.isIncreasing)
},
like() {
console.log(this)
this.setProps({likes: this.props.likes + 1})
},
unlike() {
this.setProps({likes: this.props.likes - 1})
},
render: function() {
this._logPropsAndState("render()")
return (
<div>
<Button onClick={this.like}><GlyphIcon icon='thumbs-up'/> Like</Button>
<Button onClick={this.unlike}><GlyphIcon icon='thumbs-down'/> Unlike</Button>
<br/>
Likes {this.props.likes}
<GlyphIcon
icon={(this.state.isIncreasing)
? 'circle-arrow-up' : 'circle-arrow-down'}/>
</div>
)
}
})
ReactDOM.render(<HelloReact />, document.getElementById('app'))
</script>
</body>
</html>