-
Notifications
You must be signed in to change notification settings - Fork 45.6k
/
index.js
173 lines (159 loc) · 3.88 KB
/
index.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
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
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import * as React from 'react';
import {
Fragment,
Suspense,
unstable_SuspenseList as SuspenseList,
useState,
} from 'react';
function SuspenseTree() {
return (
<Fragment>
<h1>Suspense</h1>
<h4>Primary to Fallback Cycle</h4>
<PrimaryFallbackTest initialSuspend={false} />
<h4>Fallback to Primary Cycle</h4>
<PrimaryFallbackTest initialSuspend={true} />
<NestedSuspenseTest />
<SuspenseListTest />
</Fragment>
);
}
function PrimaryFallbackTest({initialSuspend}) {
const [suspend, setSuspend] = useState(initialSuspend);
const fallbackStep = useTestSequence('fallback', Fallback1, Fallback2);
const primaryStep = useTestSequence('primary', Primary1, Primary2);
return (
<Fragment>
<label>
<input
checked={suspend}
onChange={e => setSuspend(e.target.checked)}
type="checkbox"
/>
Suspend
</label>
<br />
<Suspense fallback={fallbackStep}>
{suspend ? <Never /> : primaryStep}
</Suspense>
</Fragment>
);
}
function useTestSequence(label, T1, T2) {
const [step, setStep] = useState(0);
const next = (
<button onClick={() => setStep(s => (s + 1) % allSteps.length)}>
next {label} content
</button>
);
const allSteps = [
<Fragment>{next}</Fragment>,
<Fragment>
{next} <T1 prop={step}>mount</T1>
</Fragment>,
<Fragment>
{next} <T1 prop={step}>update</T1>
</Fragment>,
<Fragment>
{next} <T2 prop={step}>several</T2> <T1 prop={step}>different</T1>{' '}
<T2 prop={step}>children</T2>
</Fragment>,
<Fragment>
{next} <T2 prop={step}>goodbye</T2>
</Fragment>,
];
return allSteps[step];
}
function NestedSuspenseTest() {
return (
<Fragment>
<h3>Nested Suspense</h3>
<Suspense fallback={<Fallback1>Loading outer</Fallback1>}>
<Parent />
</Suspense>
</Fragment>
);
}
function Parent() {
return (
<div>
<Suspense fallback={<Fallback1>Loading inner 1</Fallback1>}>
<Primary1>Hello</Primary1>
</Suspense>{' '}
<Suspense fallback={<Fallback2>Loading inner 2</Fallback2>}>
<Primary2>World</Primary2>
</Suspense>
<br />
<Suspense fallback={<Fallback1>This will never load</Fallback1>}>
<Never />
</Suspense>
<br />
<b>
<LoadLater />
</b>
</div>
);
}
function SuspenseListTest() {
return (
<>
<h1>SuspenseList</h1>
<SuspenseList revealOrder="forwards" tail="collapsed">
<div>
<Suspense fallback={<Fallback1>Loading 1</Fallback1>}>
<Primary1>Hello</Primary1>
</Suspense>
</div>
<div>
<LoadLater />
</div>
<div>
<Suspense fallback={<Fallback2>Loading 2</Fallback2>}>
<Primary2>World</Primary2>
</Suspense>
</div>
</SuspenseList>
</>
);
}
function LoadLater() {
const [loadChild, setLoadChild] = useState(false);
return (
<Suspense
fallback={
<Fallback1 onClick={() => setLoadChild(true)}>Click to load</Fallback1>
}>
{loadChild ? (
<Primary1 onClick={() => setLoadChild(false)}>
Loaded! Click to suspend again.
</Primary1>
) : (
<Never />
)}
</Suspense>
);
}
function Never() {
throw new Promise(resolve => {});
}
function Fallback1({prop, ...rest}: any) {
return <span {...rest} />;
}
function Fallback2({prop, ...rest}: any) {
return <span {...rest} />;
}
function Primary1({prop, ...rest}: any) {
return <span {...rest} />;
}
function Primary2({prop, ...rest}: any) {
return <span {...rest} />;
}
export default SuspenseTree;