Skip to content

Commit 4f5fdd2

Browse files
committed
Add basic fetch example.
1 parent 8554d23 commit 4f5fdd2

File tree

6 files changed

+115
-0
lines changed

6 files changed

+115
-0
lines changed

examples/basic-fetch/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-fetch)
2+
3+
# Basic fetch with React Async
4+
5+
This demonstrates a very simple HTTP GET using `fetch`, wrapped with React Async.

examples/basic-fetch/package.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "basic-fetch",
3+
"version": "0.0.0",
4+
"private": true,
5+
"dependencies": {
6+
"react": "^16.7.0",
7+
"react-async": "latest",
8+
"react-dom": "^16.7.0",
9+
"react-scripts": "2.1.2"
10+
},
11+
"scripts": {
12+
"start": "react-scripts start",
13+
"build": "react-scripts build",
14+
"test": "react-scripts test",
15+
"eject": "react-scripts eject"
16+
},
17+
"eslintConfig": {
18+
"extends": "react-app"
19+
},
20+
"browserslist": [
21+
">0.2%",
22+
"not dead",
23+
"not ie <= 11",
24+
"not op_mini all"
25+
]
26+
}
3.78 KB
Binary file not shown.
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-fetch/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-fetch/src/index.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import React from "react"
2+
import Async 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+
11+
const UserPlaceholder = () => (
12+
<div className="user placeholder">
13+
<div className="avatar" />
14+
<div className="name">══════</div>
15+
</div>
16+
)
17+
18+
const UserDetails = ({ data }) => (
19+
<div className="user">
20+
<img className="avatar" src={data.data.avatar} alt="" />
21+
<div className="name">
22+
{data.data.first_name} {data.data.last_name}
23+
</div>
24+
</div>
25+
)
26+
27+
const App = () => (
28+
<>
29+
<Async promiseFn={loadUser} userId={1}>
30+
{({ data, isLoading }) => (isLoading ? <UserPlaceholder /> : <UserDetails data={data} />)}
31+
</Async>
32+
33+
<Async promiseFn={loadUser} userId={2}>
34+
<Async.Loading>
35+
<UserPlaceholder />
36+
</Async.Loading>
37+
<Async.Resolved>{data => <UserDetails data={data} />}</Async.Resolved>
38+
</Async>
39+
</>
40+
)
41+
42+
ReactDOM.render(<App />, document.getElementById("root"))

0 commit comments

Comments
 (0)