forked from RubyLouvre/anu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex6.html
95 lines (81 loc) · 2.58 KB
/
index6.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<script type='text/javascript' src="./dist/React.js"></script>
<!-- <script type='text/javascript' src="./react.js"></script>
<script type='text/javascript' src="./react-dom.js"></script>-->
<script type='text/javascript' src="./lib/ReactPropTypes.js"></script>
<script type='text/javascript' src="./lib/babel.js"></script>
</head>
<body>
<div id='root' class="root">
</div>
<script type='text/babel'>
// bug说明:更新Hello的state不应该触发componentWillReceiveProps。
if (!window.ReactDOM) {
window.ReactDOM = React;
}
var PropTypes = ReactPropTypes
class ListViewDemo extends React.Component {
static childContextTypes = {
testItems: PropTypes.array
}
constructor(props) {
super(props);
this.state = {
testItems: [1,2,3]
};
}
getChildContext() {
return { testItems: this.state.testItems };
}
render() {
return (
<div>
<ul>
{this.state.testItems.map(function(el){
return <li>{el}</li>
})}
</ul>
<Hello />
</div>
);
}
}
console.log("======")
class Hello extends React.Component {
static contextTypes = {
testItems: PropTypes.array
}
constructor(props) {
super(props);
this.state = {
hello: "world"
};
}
componentWillReceiveProps(nextProps) {
console.log("componentWillReceiveProps");
console.log(this.context);
}
componentDidUpdate() {
console.log("componentDidUpdate");
console.log(this.context);
}
changeHello() {
this.setState({
hello: "woooo"
});
}
render() {
return (
<div onClick={this.changeHello.bind(this)}>
{this.state.hello}
</div>
);
}
}
ReactDOM.render(<ListViewDemo />, document.getElementById('root'));
</script>
</html>