-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
dynamic.js
56 lines (51 loc) · 1.15 KB
/
dynamic.js
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
import { Component } from 'react';
import dynamic from 'umi/dynamic';
const delay = timeout => new Promise(resolve => setTimeout(resolve, timeout));
const A = dynamic(async function() {
await delay(/* 1s */ 1000);
return () => <div>A rendered after 1s</div>;
});
const B = dynamic(
async function() {
await delay(/* 1s */ 1000);
return () => <div>B rendered after 1s</div>;
},
{
loading() {
return <div>Custome Loading...</div>;
},
},
);
export default class Dynamic extends Component {
constructor(props) {
super(props);
this.state = {
A: false,
B: false,
};
}
render() {
return (
<div>
<h2>{`dynamic(resolve)`}</h2>
<button
onClick={() => {
this.setState({ A: true });
}}
>
loadA
</button>
{this.state.A ? <A /> : <div>A is not loaded</div>}
<h2>{`dynamic(resolve, { loading })`}</h2>
<button
onClick={() => {
this.setState({ B: true });
}}
>
loadB
</button>
{this.state.B ? <B /> : <div>B is not loaded</div>}
</div>
);
}
}