-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathindex-react.html
99 lines (91 loc) · 2.76 KB
/
index-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
<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div id="example"></div>
<script type="text/babel">
var sexMap = {
'true': "男",
'false': "女"
}
function genData(n) {
var ret = []
for (var i = 0; i < n; i++) {
ret.push({
name: Math.random(),
age: 3 + Math.ceil((Math.random() * 30)),
sex: sexMap[1 - Math.random() > 0.5],
desc: Math.random()
})
}
return ret
}
var array = []
var time = 0, id, now
var Table = React.createClass({
getInitialState: function() {
now = new Date
return {array: genData(1000)}
},
componentDidUpdate:function(){
var cost = new Date - now
console.log('total ' + (cost) + ' ms!', time++)
array.push(cost)
var that = this
if(array.length > 60){
console.log('平均耗时 ' + (array.reduce(function (a, b) {
return a + b
}, 0)) / array.length + ' ms!!!')
array.shift()
console.log('平均耗时 ' + (array.reduce(function (a, b) {
return a + b
}, 0)) / array.length + ' ms!!!')
}else{
setTimeout(function(){
now = new Date - 0
that.setState({
array: genData(1000)
})
},100)
}
},
componentDidMount: function(){
var cost = new Date - now
console.log('total ' + (cost) + ' ms', time++)
array.push(cost)
this.setState({
array: genData(1000)
})
},
render: function() {
return <table style={{border:'1px solid black'}}>
<tbody>
{
this.state.array.map(function(obj,index){
return (<tr key={index}>
{
Object.keys(obj).slice(0,3).map(function(key){
return <td data-align={key === 'age'? 'left':'right'} key={key}>{obj[key]}</td>
})
}
</tr>)
})
}
</tbody>
</table>
}
});
ReactDOM.render(
<Table />,
document.getElementById('example')
);
</script>
<script src="//cdn.bootcss.com/react/15.2.1/react.js"></script>
<script src="//cdn.bootcss.com/react/15.2.1/react-dom.js"></script>
<script src="//cdn.bootcss.com/babel-core/5.8.34/browser.js"></script>
</body>
</html>