Skip to content

Commit 14268c5

Browse files
committed
Add basic useAsync example.
1 parent a009154 commit 14268c5

File tree

8 files changed

+139
-0
lines changed

8 files changed

+139
-0
lines changed

examples/basic-hook/.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SKIP_PREFLIGHT_CHECK=true

examples/basic-hook/.gitignore

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# production
12+
/build
13+
14+
# misc
15+
.DS_Store
16+
.env.local
17+
.env.development.local
18+
.env.test.local
19+
.env.production.local
20+
21+
npm-debug.log*
22+
yarn-debug.log*
23+
yarn-error.log*

examples/basic-hook/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[![Deploy to now](https://deploy.now.sh/static/button.svg)](https://deploy.now.sh/?repo=https://github.com/ghengeveld/react-async/tree/master/examples/basic-hook)
2+
3+
# Basic fetch with useAsync hook
4+
5+
This demonstrates how to use the `useAsync` hook.

examples/basic-hook/package.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "basic-hook",
3+
"version": "0.0.0",
4+
"private": true,
5+
"dependencies": {
6+
"react": "16.7.0-alpha.2",
7+
"react-async": "3.11.0",
8+
"react-dom": "16.7.0-alpha.2",
9+
"react-scripts": "2.1.2"
10+
},
11+
"scripts": {
12+
"start": "react-scripts start",
13+
"build": "react-scripts build",
14+
"now-build": "npm run build && mv build dist"
15+
},
16+
"eslintConfig": {
17+
"extends": "react-app"
18+
},
19+
"browserslist": [
20+
">0.2%",
21+
"not dead",
22+
"not ie <= 11",
23+
"not op_mini all"
24+
]
25+
}
3.78 KB
Binary file not shown.

examples/basic-hook/public/index.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
6+
<meta name="theme-color" content="#000000" />
7+
<title>React App</title>
8+
</head>
9+
<body>
10+
<noscript> You need to enable JavaScript to run this app. </noscript>
11+
<div id="root"></div>
12+
</body>
13+
</html>

examples/basic-hook/src/index.css

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
body {
2+
margin: 20px;
3+
padding: 0;
4+
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", "Ubuntu",
5+
"Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif;
6+
-webkit-font-smoothing: antialiased;
7+
-moz-osx-font-smoothing: grayscale;
8+
}
9+
10+
.user {
11+
display: inline-block;
12+
margin: 20px;
13+
text-align: center;
14+
}
15+
16+
.avatar {
17+
background: #eee;
18+
border-radius: 64px;
19+
width: 128px;
20+
height: 128px;
21+
}
22+
23+
.name {
24+
margin-top: 10px;
25+
}
26+
27+
.placeholder {
28+
opacity: 0.5;
29+
}

examples/basic-hook/src/index.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import React from "react"
2+
import { useAsync } from "react-async"
3+
import ReactDOM from "react-dom"
4+
import "./index.css"
5+
6+
const loadUser = ({ userId }) =>
7+
fetch(`https://reqres.in/api/users/${userId}`)
8+
.then(res => (res.ok ? res : Promise.reject(res)))
9+
.then(res => res.json())
10+
.then(({ data }) => data)
11+
12+
const UserPlaceholder = () => (
13+
<div className="user placeholder">
14+
<div className="avatar" />
15+
<div className="name">══════</div>
16+
</div>
17+
)
18+
19+
const UserDetails = ({ data }) => (
20+
<div className="user">
21+
<img className="avatar" src={data.avatar} alt="" />
22+
<div className="name">
23+
{data.first_name} {data.last_name}
24+
</div>
25+
</div>
26+
)
27+
28+
const User = ({ userId }) => {
29+
const { data, error, isLoading } = useAsync({ promiseFn: loadUser, userId })
30+
if (isLoading) return <UserPlaceholder />
31+
if (error) return <p>{error.message}</p>
32+
if (data) return <UserDetails data={data} />
33+
return null
34+
}
35+
36+
const App = () => (
37+
<>
38+
<User userId={1} />
39+
<User userId={2} />
40+
</>
41+
)
42+
43+
ReactDOM.render(<App />, document.getElementById("root"))

0 commit comments

Comments
 (0)