-
-
Notifications
You must be signed in to change notification settings - Fork 625
/
Copy pathsuspense.tsx
46 lines (37 loc) · 794 Bytes
/
suspense.tsx
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
import React from 'react';
import {render, Text} from '../../src/index.js';
let promise: Promise<void> | undefined;
let state: string | undefined;
let value: string | undefined;
const read = () => {
if (!promise) {
promise = new Promise(resolve => {
setTimeout(resolve, 500);
});
state = 'pending';
(async () => {
await promise;
state = 'done';
value = 'Hello World';
})();
}
if (state === 'pending') {
// eslint-disable-next-line @typescript-eslint/only-throw-error
throw promise;
}
if (state === 'done') {
return value;
}
};
function Example() {
const message = read();
return <Text>{message}</Text>;
}
function Fallback() {
return <Text>Loading...</Text>;
}
render(
<React.Suspense fallback={<Fallback />}>
<Example />
</React.Suspense>,
);