-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2025 from preactjs/fix/suspense-fallback
Support re-suspending
- Loading branch information
Showing
17 changed files
with
455 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { createElement } from "react"; | ||
|
||
export default function AddNewComponent({appearance}) { | ||
return <div>AddNewComponent (component #{appearance})</div>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { createElement, lazy } from "react"; | ||
|
||
const pause = timeout => | ||
new Promise(d => setTimeout(d, timeout), console.log(timeout)); | ||
|
||
const SubComponent = lazy(() => | ||
pause(Math.random() * 1000).then(() => import("./subcomponent.js")) | ||
); | ||
|
||
export default function ComponentContainer({ appearance }) { | ||
return ( | ||
<div> | ||
GenerateComponents (component #{appearance}) | ||
<SubComponent /> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { createElement } from "react"; | ||
|
||
export default function DropZone({appearance}) { | ||
return <div>DropZone (component #{appearance})</div>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { createElement } from "react"; | ||
|
||
export default function Editor({ children }) { | ||
return <div className="Editor">{children}</div>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { createElement, Suspense, lazy, Component } from "react"; | ||
|
||
const Loading = function() { | ||
return <div>Loading...</div>; | ||
}; | ||
const Error = function({ resetState }) { | ||
return ( | ||
<div> | ||
Error! | ||
<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> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { createElement } from "react"; | ||
|
||
export default function SubComponent({ onClick }) { | ||
return <div>Lazy loaded sub component</div>; | ||
} |
Oops, something went wrong.