-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add experimental Suspense support (#153)
* Add experimental Suspense support. * Add PropType and type definition. * Skip Suspense test when running against 16.3. * Lock down all version ranges. * Fix eslint config. * Disable rules of hooks for examples. * Attempt at fixing CircleCI memory issue. * Update lockfile. * Bump deps. * Revert "Disable rules of hooks for examples." This reverts commit d3d931a.
- Loading branch information
1 parent
11f8996
commit 4cfaa0c
Showing
15 changed files
with
219 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
SKIP_PREFLIGHT_CHECK=true |
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,7 @@ | ||
# Basic fetch with Suspense | ||
|
||
This demonstrates how Suspense can be used to render a fallback UI while loading. | ||
|
||
<a href="https://react-async.async-library.now.sh/examples/with-suspense"> | ||
<img src="https://img.shields.io/badge/live-demo-blue.svg" alt="live demo"> | ||
</a> |
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,42 @@ | ||
{ | ||
"name": "with-suspense-example", | ||
"version": "8.0.0", | ||
"private": true, | ||
"homepage": "https://react-async.async-library.now.sh/examples/with-suspense", | ||
"scripts": { | ||
"postinstall": "relative-deps", | ||
"prestart": "relative-deps", | ||
"prebuild": "relative-deps", | ||
"pretest": "relative-deps", | ||
"start": "react-scripts start", | ||
"build": "react-scripts build", | ||
"test": "react-scripts test", | ||
"now-build": "SKIP_PREFLIGHT_CHECK=true react-scripts build" | ||
}, | ||
"dependencies": { | ||
"react": "16.10.1", | ||
"react-async": "8.0.0", | ||
"react-async-devtools": "8.0.0", | ||
"react-dom": "16.10.1", | ||
"react-scripts": "3.1.2" | ||
}, | ||
"devDependencies": { | ||
"relative-deps": "0.1.2" | ||
}, | ||
"relativeDependencies": { | ||
"react-async": "../../packages/react-async/pkg", | ||
"react-async-devtools": "../../packages/react-async-devtools/pkg" | ||
}, | ||
"eslintConfig": { | ||
"extends": "react-app" | ||
}, | ||
"browserslist": [ | ||
">0.2%", | ||
"not dead", | ||
"not ie <= 11", | ||
"not op_mini all" | ||
], | ||
"engines": { | ||
"node": ">=8" | ||
} | ||
} |
Binary file not shown.
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,13 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /> | ||
<meta name="theme-color" content="#000000" /> | ||
<title>React App</title> | ||
</head> | ||
<body> | ||
<noscript> You need to enable JavaScript to run this app. </noscript> | ||
<div id="root"></div> | ||
</body> | ||
</html> |
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,29 @@ | ||
body { | ||
margin: 20px; | ||
padding: 0; | ||
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", "Ubuntu", | ||
"Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif; | ||
-webkit-font-smoothing: antialiased; | ||
-moz-osx-font-smoothing: grayscale; | ||
} | ||
|
||
.user { | ||
display: inline-block; | ||
margin: 20px; | ||
text-align: center; | ||
} | ||
|
||
.avatar { | ||
background: #eee; | ||
border-radius: 64px; | ||
width: 128px; | ||
height: 128px; | ||
} | ||
|
||
.name { | ||
margin-top: 10px; | ||
} | ||
|
||
.placeholder { | ||
opacity: 0.5; | ||
} |
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,61 @@ | ||
import React, { Suspense } from "react" | ||
import { useAsync, IfFulfilled, IfRejected } from "react-async" | ||
import ReactDOM from "react-dom" | ||
import DevTools from "react-async-devtools" | ||
import "./index.css" | ||
|
||
const loadUser = ({ userId }) => | ||
fetch(`https://reqres.in/api/users/${userId}`) | ||
.then(res => (res.ok ? res : Promise.reject(res))) | ||
.then(res => res.json()) | ||
.then(({ data }) => data) | ||
|
||
const UserPlaceholder = () => ( | ||
<div className="user placeholder"> | ||
<div className="avatar" /> | ||
<div className="name">══════</div> | ||
</div> | ||
) | ||
|
||
const UserDetails = ({ data }) => ( | ||
<div className="user"> | ||
<img className="avatar" src={data.avatar} alt="" /> | ||
<div className="name"> | ||
{data.first_name} {data.last_name} | ||
</div> | ||
</div> | ||
) | ||
|
||
const User = ({ userId }) => { | ||
const state = useAsync({ | ||
suspense: true, | ||
promiseFn: loadUser, | ||
debugLabel: `User ${userId}`, | ||
userId, | ||
}) | ||
return ( | ||
<> | ||
<IfFulfilled state={state}>{data => <UserDetails data={data} />}</IfFulfilled> | ||
<IfRejected state={state}>{error => <p>{error.message}</p>}</IfRejected> | ||
</> | ||
) | ||
} | ||
|
||
export const App = () => ( | ||
<> | ||
<DevTools /> | ||
<Suspense | ||
fallback={ | ||
<> | ||
<UserPlaceholder /> | ||
<UserPlaceholder /> | ||
</> | ||
} | ||
> | ||
<User userId={1} /> | ||
<User userId={2} /> | ||
</Suspense> | ||
</> | ||
) | ||
|
||
if (process.env.NODE_ENV !== "test") ReactDOM.render(<App />, document.getElementById("root")) |
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,9 @@ | ||
import React from "react" | ||
import ReactDOM from "react-dom" | ||
import { App } from "./" | ||
|
||
it("renders without crashing", () => { | ||
const div = document.createElement("div") | ||
ReactDOM.render(<App />, div) | ||
ReactDOM.unmountComponentAtNode(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
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