-
Notifications
You must be signed in to change notification settings - Fork 0
/
example8-ExposeComponentFunctions.html
65 lines (57 loc) · 1.59 KB
/
example8-ExposeComponentFunctions.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>example2</title>
<script src="build/react.js"></script>
<script src="build/react-dom.js"></script>
<script src="build/browser.js"></script>
<script src="build/jquery.js"></script>
<script src="build/remarkable.js"></script>
</head>
<body>
<div id="example"> </div>
<div id="example2"> </div>
<script type="text/babel">
var Todo = React.createClass({
render: function() {
return <div onClick={this.props.onClick}>{this.props.title}</div>;
},
//this component will be accessed by the parent through the `ref` attribute
animate: function() {
console.log('Pretend %s is animating', this.props.title);
}
});
var Todos = React.createClass({
getInitialState: function() {
return {items: ['Apple', 'Banana', 'Cranberry']};
},
handleClick: function(index) {
var items = this.state.items.filter(function(item, i) {
return index !== i;
});
this.setState({items: items}, function() {
if (items.length === 1) {
this.refs.item0.animate();
}
}.bind(this));
},
render: function() {
return (
<div>
{this.state.items.map(function(item, i) {
var boundClick = this.handleClick.bind(this, i);
return (
<Todo onClick={boundClick} key={i} title={item} ref={'item' + i} />
);
}, this)}
</div>
);
}
});
ReactDOM.render(<Todos />,
document.getElementById('example')
);
</script>
</body>
</html>