-
Notifications
You must be signed in to change notification settings - Fork 6
/
props-state-02.html
50 lines (47 loc) · 1.54 KB
/
props-state-02.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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv='Content-type' content='text/html; charset=utf-8'>
<title>Basic Example Props</title>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.3/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.3/JSXTransformer.js"></script>
<script type="text/jsx">
var Avatar = React.createClass({
propTypes: {
name: React.PropTypes.string.isRequired,
width: React.PropTypes.number.isRequired,
height: React.PropTypes.number.isRequired,
initialLike: React.PropTypes.bool.isRequired,
},
getInitialState() {
return {
liked: this.props.initialLike
};
},
onClick() {
this.setState({liked: !this.state.liked});
},
render() {
var textLike = this.state.liked ? 'like' : 'haven\'t liked';
return (
<div>
<img src={this.props.src} width={this.props.width} height={this.props.height} alt="alt" />
<span>{this.props.name}</span>
<button onClick={this.onClick}>{textLike}</button>
</div>
);
}
});
var AvatarEl = <Avatar
name="Foo"
width={100}
height={100}
src='http://canime.files.wordpress.com/2010/05/mask-dtb.jpg'
initialLike={false}
/>;
var AvatarComponent = React.render(AvatarEl, document.body);
</script>
</body>
</html>