forked from RubyLouvre/anu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex5.html
420 lines (403 loc) · 12.4 KB
/
index5.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
<!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/ReactTestUtils.js"></script>
<script type='text/javascript' src="./lib/babel.js"></script>
</head>
<body>
<pre>React 15 </pre>
<div id='root' class="root">
</div>
<script type='text/babel'>
var container = document.getElementById('root');
var div = container;
if (!window.ReactDOM) {
window.ReactDOM = React;
}
var expect = function(a) {
return {
toBe: function(b) {
console.log(a, 'vs', b, a === b);
},
toEqual(b) {
console.log(a, 'vs', b, a + '' === b + '');
},
toThrow(){
try{
a()
}catch(e){
console.log(e,"catch")
}
}
};
};
var log = {
push(a){
console.log(a)
}
}
// var log = []
var BrokenRender = class extends React.Component {
constructor(props) {
super(props);
log.push('BrokenRender constructor');
}
render() {
log.push('BrokenRender render [!]');
throw new Error('Hello');
}
UNSAFE_componentWillMount() {
log.push('BrokenRender componentWillMount');
}
componentDidMount() {
log.push('BrokenRender componentDidMount');
}
UNSAFE_componentWillReceiveProps() {
log.push('BrokenRender componentWillReceiveProps');
}
UNSAFE_componentWillUpdate() {
log.push('BrokenRender componentWillUpdate');
}
componentDidUpdate() {
log.push('BrokenRender componentDidUpdate');
}
componentWillUnmount() {
log.push('BrokenRender componentWillUnmount');
}
};
var BrokenComponentWillMount = class extends React.Component {
constructor(props) {
super(props);
log.push('BrokenComponentWillMount constructor');
}
render() {
log.push('BrokenComponentWillMount render');
return <div>{this.props.children}</div>;
}
UNSAFE_componentWillMount() {
log.push('BrokenComponentWillMount componentWillMount [!]');
throw new Error('Hello');
}
componentDidMount() {
log.push('BrokenComponentWillMount componentDidMount');
}
UNSAFE_componentWillReceiveProps() {
log.push('BrokenComponentWillMount componentWillReceiveProps');
}
UNSAFE_componentWillUpdate() {
log.push('BrokenComponentWillMount componentWillUpdate');
}
componentDidUpdate() {
log.push('BrokenComponentWillMount componentDidUpdate');
}
componentWillUnmount() {
log.push('BrokenComponentWillMount componentWillUnmount');
}
};
var ErrorMessage = class extends React.Component {
constructor(props) {
super(props);
log.push('ErrorMessage constructor');
}
UNSAFE_componentWillMount() {
log.push('ErrorMessage componentWillMount');
}
componentDidMount() {
log.push('ErrorMessage componentDidMount');
}
componentWillUnmount() {
log.push('ErrorMessage componentWillUnmount');
}
render() {
log.push('ErrorMessage render');
return <div>Caught an error: {this.props.message}.</div>;
}
};
var ErrorBoundary = class extends React.Component {
constructor(props) {
super(props);
this.state = { error: null };
log.push(`${this.props.logName} constructor`);
}
render() {
if (this.state.error && !this.props.forceRetry) {
log.push(`${this.props.logName} render error`);
return this.props.renderError(this.state.error, this.props);
}
log.push(`${this.props.logName} render success`);
return <div>{this.props.children}</div>;
}
componentDidCatch(error) {
log.push(`${this.props.logName} componentDidCatch`);
this.setState({ error });
}
UNSAFE_componentWillMount() {
log.push(`${this.props.logName} componentWillMount`);
}
componentDidMount() {
log.push(`${this.props.logName} componentDidMount`);
}
UNSAFE_componentWillReceiveProps() {
log.push(`${this.props.logName} componentWillReceiveProps`);
}
UNSAFE_componentWillUpdate() {
log.push(`${this.props.logName} componentWillUpdate`);
}
componentDidUpdate() {
log.push(`${this.props.logName} componentDidUpdate`);
}
componentWillUnmount() {
log.push(`${this.props.logName} componentWillUnmount`);
}
};
ErrorBoundary.defaultProps = {
logName: 'ErrorBoundary',
renderError(error, props) {
return <div ref={props.errorMessageRef}>Caught an error: {error.message}.</div>;
},
};
var RetryErrorBoundary = class extends React.Component {
constructor(props) {
super(props);
log.push('RetryErrorBoundary constructor');
}
render() {
log.push('RetryErrorBoundary render');
return <BrokenRender />;
}
UNSAFE_componentWillMount() {
log.push('RetryErrorBoundary componentWillMount');
}
componentDidMount() {
log.push('RetryErrorBoundary componentDidMount');
}
componentWillUnmount() {
log.push('RetryErrorBoundary componentWillUnmount');
}
componentDidCatch(error) {
log.push('RetryErrorBoundary componentDidCatch [!]');
// In Fiber, calling setState() (and failing) is treated as a rethrow.
this.setState({});
}
};
var Normal = class extends React.Component {
static defaultProps = {
logName: 'Normal',
};
constructor(props) {
super(props);
log.push(`${this.props.logName} constructor`);
}
render() {
log.push(`${this.props.logName} render`);
return <div>{this.props.children}</div>;
}
UNSAFE_componentWillMount() {
log.push(`${this.props.logName} componentWillMount`);
}
componentDidMount() {
log.push(`${this.props.logName} componentDidMount`);
}
UNSAFE_componentWillReceiveProps() {
log.push(`${this.props.logName} componentWillReceiveProps`);
}
UNSAFE_componentWillUpdate() {
log.push(`${this.props.logName} componentWillUpdate`);
}
componentDidUpdate() {
log.push(`${this.props.logName} componentDidUpdate`);
}
componentWillUnmount() {
log.push(`${this.props.logName} componentWillUnmount`);
}
};
var BrokenComponentWillUnmount = class extends React.Component {
static defaultProps = {
errorText: 'Hello',
};
constructor(props) {
super(props);
log.push('BrokenComponentWillUnmount constructor');
}
render() {
log.push('BrokenComponentWillUnmount render');
return <div>{this.props.children}</div>;
}
UNSAFE_componentWillMount() {
log.push('BrokenComponentWillUnmount componentWillMount');
}
componentDidMount() {
log.push('BrokenComponentWillUnmount componentDidMount');
}
UNSAFE_componentWillReceiveProps() {
log.push('BrokenComponentWillUnmount componentWillReceiveProps');
}
UNSAFE_componentWillUpdate() {
log.push('BrokenComponentWillUnmount componentWillUpdate');
}
componentDidUpdate() {
log.push('BrokenComponentWillUnmount componentDidUpdate');
}
componentWillUnmount() {
log.push('BrokenComponentWillUnmount componentWillUnmount [!]');
throw new Error(this.props.errorText);
}
};
var BrokenComponentWillMountErrorBoundary = class extends React.Component {
constructor(props) {
super(props);
this.state = { error: null };
log.push('BrokenComponentWillMountErrorBoundary constructor');
}
render() {
if (this.state.error) {
log.push('BrokenComponentWillMountErrorBoundary render error');
return <div>Caught an error: {this.state.error.message}.</div>;
}
log.push('BrokenComponentWillMountErrorBoundary render success');
return <div>{this.props.children}</div>;
}
UNSAFE_componentWillMount() {
log.push('BrokenComponentWillMountErrorBoundary componentWillMount [!]');
throw new Error('Hello');
}
componentDidMount() {
log.push('BrokenComponentWillMountErrorBoundary componentDidMount');
}
componentWillUnmount() {
log.push('BrokenComponentWillMountErrorBoundary componentWillUnmount');
}
componentDidCatch(error) {
log.push('BrokenComponentWillMountErrorBoundary componentDidCatch');
this.setState({ error });
}
};
var BrokenRenderErrorBoundary = class extends React.Component {
constructor(props) {
super(props);
this.state = { error: null };
log.push('BrokenRenderErrorBoundary constructor');
}
render() {
if (this.state.error) {
log.push('BrokenRenderErrorBoundary render error [!]');
throw new Error('Hello');
}
log.push('BrokenRenderErrorBoundary render success');
return <div>{this.props.children}</div>;
}
UNSAFE_componentWillMount() {
log.push('BrokenRenderErrorBoundary componentWillMount');
}
componentDidMount() {
log.push('BrokenRenderErrorBoundary componentDidMount');
}
componentWillUnmount() {
log.push('BrokenRenderErrorBoundary componentWillUnmount');
}
componentDidCatch(error) {
log.push('BrokenRenderErrorBoundary componentDidCatch');
this.setState({ error });
}
};
var BrokenConstructor = class extends React.Component {
constructor(props) {
super(props);
log.push('BrokenConstructor constructor [!]');
throw new Error('Hello');
}
render() {
log.push('BrokenConstructor render');
return <div>{this.props.children}</div>;
}
UNSAFE_componentWillMount() {
log.push('BrokenConstructor componentWillMount');
}
componentDidMount() {
log.push('BrokenConstructor componentDidMount');
}
UNSAFE_componentWillReceiveProps() {
log.push('BrokenConstructor componentWillReceiveProps');
}
UNSAFE_componentWillUpdate() {
log.push('BrokenConstructor componentWillUpdate');
}
componentDidUpdate() {
log.push('BrokenConstructor componentDidUpdate');
}
componentWillUnmount() {
log.push('BrokenConstructor componentWillUnmount');
}
};
function renderInnerError(error) {
return <div>Caught an inner error: {error.message}.</div>;
}
function renderOuterError(error) {
return <div>Caught an outer error: {error.message}.</div>;
}
// const container = document.createElement('div');
ReactDOM.render(
<ErrorBoundary
logName="OuterErrorBoundary"
renderError={renderOuterError}>
<ErrorBoundary
logName="InnerErrorBoundary"
renderError={renderInnerError}>
<BrokenComponentWillUnmount />
</ErrorBoundary>
</ErrorBoundary>,
container,
);
log.length = 0;
ReactDOM.render(
<ErrorBoundary
logName="OuterErrorBoundary"
renderError={renderOuterError}>
<ErrorBoundary
logName="InnerErrorBoundary"
renderError={renderInnerError}
/>
</ErrorBoundary>,
container,
);
expect(container.textContent).toBe('Caught an inner error: Hello.');
expect(log).toEqual([
// Update outer boundary
'OuterErrorBoundary componentWillReceiveProps',
'OuterErrorBoundary componentWillUpdate',
'OuterErrorBoundary render success',
// Update inner boundary
'InnerErrorBoundary componentWillReceiveProps',
'InnerErrorBoundary componentWillUpdate',
'InnerErrorBoundary render success',
// Try unmounting child
'BrokenComponentWillUnmount componentWillUnmount [!]',
// Fiber proceeds with lifecycles despite errors
// Inner and outer boundaries have updated in this phase
'InnerErrorBoundary componentDidUpdate',
'OuterErrorBoundary componentDidUpdate',
// Now that commit phase is done, Fiber handles errors
// Only inner boundary receives the error:
'InnerErrorBoundary componentDidCatch',
'InnerErrorBoundary componentWillUpdate',
// Render an error now
'InnerErrorBoundary render error',
// In Fiber, this was a local update to the
// inner boundary so only its hook fires
'InnerErrorBoundary componentDidUpdate',
]);
/*
log.length = 0;
ReactDOM.unmountComponentAtNode(container);
expect(log).toEqual([
'OuterErrorBoundary componentWillUnmount',
'InnerErrorBoundary componentWillUnmount',
]);
*/
</script>
</html>