summaryrefslogtreecommitdiff
path: root/preact/demo/nested-suspense/index.js
blob: 6c525b397d865fabe5961f5aea46370a06029e74 (plain)
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
import { createElement, Suspense, lazy, Component } from 'react';

const Loading = function() {
	return <div>Loading...</div>;
};
const Error = function({ resetState }) {
	return (
		<div>
			Error!&nbsp;
			<a onClick={resetState} href="#">
				Reset app
			</a>
		</div>
	);
};

const pause = timeout =>
	new Promise(d => setTimeout(d, timeout), console.log(timeout));

const DropZone = lazy(() =>
	pause(Math.random() * 1000).then(() => import('./dropzone.js'))
);
const Editor = lazy(() =>
	pause(Math.random() * 1000).then(() => import('./editor.js'))
);
const AddNewComponent = lazy(() =>
	pause(Math.random() * 1000).then(() => import('./addnewcomponent.js'))
);
const GenerateComponents = lazy(() =>
	pause(Math.random() * 1000).then(() => import('./component-container.js'))
);

export default class App extends Component {
	state = { hasError: false };

	static getDerivedStateFromError(error) {
		// Update state so the next render will show the fallback UI.
		console.warn(error);
		return { hasError: true };
	}

	render() {
		return this.state.hasError ? (
			<Error resetState={() => this.setState({ hasError: false })} />
		) : (
			<Suspense fallback={<Loading />}>
				<DropZone appearance={0} />
				<Editor title="APP_TITLE">
					<main>
						<Suspense fallback={<Loading />}>
							<GenerateComponents appearance={1} />
						</Suspense>
						<AddNewComponent appearance={2} />
					</main>
					<aside>
						<section>
							<Suspense fallback={<Loading />}>
								<GenerateComponents appearance={3} />
							</Suspense>
							<AddNewComponent appearance={4} />
						</section>
					</aside>
				</Editor>

				<footer>Footer here</footer>
			</Suspense>
		);
	}
}