-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathsuspense.tsx
More file actions
53 lines (44 loc) · 1.14 KB
/
suspense.tsx
File metadata and controls
53 lines (44 loc) · 1.14 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
import * as React from '../../src/index.cjs';
interface LazyProps {
isProp: boolean;
}
const IsLazyFunctional = (props: LazyProps) => (
<div>{props.isProp ? 'Super Lazy TRUE' : 'Super Lazy FALSE'}</div>
);
const FallBack = () => <div>Still working...</div>;
/**
* Have to mock dynamic import as import() throws a syntax error in the test runner
*/
const componentPromise = new Promise<{ default: typeof IsLazyFunctional }>(
resolve => {
setTimeout(() => {
resolve({ default: IsLazyFunctional });
}, 800);
}
);
/**
* For usage with import:
* const IsLazyComp = lazy(() => import('./lazy'));
*/
const IsLazyFunc = React.lazy(() => componentPromise);
// Suspense using lazy component
class ReactSuspensefulFunc extends React.Component {
render() {
return (
<React.Suspense fallback={<FallBack />}>
<IsLazyFunc isProp={false} />
</React.Suspense>
);
}
}
const Comp = () => <p>Hello world</p>;
const importComponent = async () => {
return { MyComponent: Comp };
};
const Lazy = React.lazy(() =>
importComponent().then(mod => ({ default: mod.MyComponent }))
);
// eslint-disable-next-line
function App() {
return <Lazy />;
}