forked from RubyLouvre/anu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex4.html
More file actions
144 lines (131 loc) · 4.26 KB
/
index4.html
File metadata and controls
144 lines (131 loc) · 4.26 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<!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="./test/react.js"></script>
<script type='text/javascript' src="./test/react-dom.js"></script>-->
<!-- <script src="./test/react-lite.js"></script>-->
<script type='text/javascript' src="./lib/babel.js"></script>
</head>
<body>
<div>开发者工具</div>
<div id='example'></div>
<pre>
['parent-render', 'blue'],
['getInitialState', 'blue'],
['render', 'dark blue', 'blue'],
['handleHue', 'dark blue', 'blue'],
['parent-handleColor', 'blue'],
['parent-render', 'green'],
['setState-this', 'dark blue', 'blue'],
['setState-args', 'dark blue', 'green'],
['render', 'light green', 'green'],
['after-setState', 'light green', 'green'],
['parent-after-setState', 'green'],
</pre>
<script type='text/babel'>
var list = []
function logger(){
list.push([].slice.call(arguments, 0))
}
class ParentComponent extends React.Component {
state = {color: 'blue'};
handleColor = color => {
this.props.logger('parent-handleColor', this.state.color);
//父方法暂时不处理
this.setState({color: color}, function() {
this.props.logger('parent-after-setState', this.state.color);
});
};
componentDidUpdate(){
console.log('parent did update')
}
componentDidMount(){
console.log('parent did mount')
}
render() {
console.log('父组件 render')
this.props.logger('parent-render', this.state.color);
return (
<ChildComponent
logger={this.props.logger}
color={this.state.color}
onSelectColor={this.handleColor}
/>
);
}
}
class ChildComponent extends React.Component {
constructor(props) {
super(props);
props.logger('getInitialState', props.color);
this.state = {hue: 'dark ' + props.color};
}
componentDidUpdate(){
console.log('child did update')
}
componentDidMount(){
console.log('child did mount')
}
handleHue = (shade, color) => {
console.log('发生了点击事件')
this.props.logger('handleHue', this.state.hue, this.props.color);
//子组件调用父组件的setState
this.props.onSelectColor(color);
console.log('子组件setState')
this.setState(
function(state, props) {
this.props.logger(
'setState-this',
this.state.hue,
this.props.color,
);
this.props.logger('setState-args', state.hue, props.color);
return {hue: shade + ' ' + props.color};
},
function() {
this.props.logger(
'after-setState',
this.state.hue,
this.props.color,
);
},
);
};
render() {
console.log('子组件 render')
this.props.logger('render', this.state.hue, this.props.color);
return (
<div>
<button onClick={this.handleHue.bind(this, 'dark', 'blue')}>
Dark Blue
</button>
<button onClick={this.handleHue.bind(this, 'light', 'blue')}>
Light Blue
</button>
<button onClick={this.handleHue.bind(this, 'dark', 'green')}>
Dark Green
</button>
<button onClick={this.handleHue.bind(this, 'light', 'green')}>
Light Green
</button>
</div>
);
}
}
var container = document.createElement('div');
document.body.appendChild(container);
void ReactDOM.render(<ParentComponent logger={logger} />, container);
if(!window.ReactDOM){
window.ReactDOM = window.React
}
console.log(container)
console.log(container.childNodes[0].childNodes[3])
container.childNodes[0].childNodes[3].click()
console.log(list)
//var s = ReactDOM.render(<App />, document.getElementById('example'))
</script>
</body>
</html>