Skip to content

Commit fde7e09

Browse files
committedOct 6, 2019
Clean up usage examples.
1 parent a0567e7 commit fde7e09

File tree

1 file changed

+33
-29
lines changed

1 file changed

+33
-29
lines changed
 

‎gitbook/usage.md

+33-29
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,21 @@ The `useAsync` hook \(available [from React v16.8.0](https://reactjs.org/hooks)\
99
```jsx
1010
import { useAsync } from "react-async"
1111

12-
const loadCustomer = async ({ customerId }, { signal }) => {
13-
const res = await fetch(`/api/customers/${customerId}`, { signal })
14-
if (!res.ok) throw new Error(res)
12+
// You can use async/await or any function that returns a Promise
13+
const loadPlayer = async ({ playerId }, { signal }) => {
14+
const res = await fetch(`/api/players/${playerId}`, { signal })
15+
if (!res.ok) throw new Error(res.statusText)
1516
return res.json()
1617
}
1718

1819
const MyComponent = () => {
19-
const { data, error, isPending } = useAsync({ promiseFn: loadCustomer, customerId: 1 })
20+
const { data, error, isPending } = useAsync({ promiseFn: loadPlayer, playerId: 1 })
2021
if (isPending) return "Loading..."
2122
if (error) return `Something went wrong: ${error.message}`
2223
if (data)
2324
return (
2425
<div>
25-
<strong>Loaded some data:</strong>
26+
<strong>Player data:</strong>
2627
<pre>{JSON.stringify(data, null, 2)}</pre>
2728
</div>
2829
)
@@ -36,7 +37,7 @@ Or using the shorthand version:
3637

3738
```jsx
3839
const MyComponent = () => {
39-
const { data, error, isPending } = useAsync(loadCustomer, options)
40+
const { data, error, isPending } = useAsync(loadPlayer, options)
4041
// ...
4142
}
4243
```
@@ -84,20 +85,21 @@ The classic interface to React Async. Simply use `<Async>` directly in your JSX
8485
import Async from "react-async"
8586

8687
// Your promiseFn receives all props from Async and an AbortController instance
87-
const loadCustomer = ({ customerId }, { signal }) =>
88-
fetch(`/api/customers/${customerId}`, { signal })
89-
.then(res => (res.ok ? res : Promise.reject(res)))
90-
.then(res => res.json())
88+
const loadPlayer = async ({ playerId }, { signal }) => {
89+
const res = await fetch(`/api/players/${playerId}`, { signal })
90+
if (!res.ok) throw new Error(res.statusText)
91+
return res.json()
92+
}
9193

9294
const MyComponent = () => (
93-
<Async promiseFn={loadCustomer} customerId={1}>
95+
<Async promiseFn={loadPlayer} playerId={1}>
9496
{({ data, error, isPending }) => {
9597
if (isPending) return "Loading..."
9698
if (error) return `Something went wrong: ${error.message}`
9799
if (data)
98100
return (
99101
<div>
100-
<strong>Loaded some data:</strong>
102+
<strong>Player data:</strong>
101103
<pre>{JSON.stringify(data, null, 2)}</pre>
102104
</div>
103105
)
@@ -116,18 +118,19 @@ You can also create your own component instances, allowing you to preconfigure t
116118
```jsx
117119
import { createInstance } from "react-async"
118120

119-
const loadCustomer = ({ customerId }, { signal }) =>
120-
fetch(`/api/customers/${customerId}`, { signal })
121-
.then(res => (res.ok ? res : Promise.reject(res)))
122-
.then(res => res.json())
121+
const loadPlayer = async ({ playerId }, { signal }) => {
122+
const res = await fetch(`/api/players/${playerId}`, { signal })
123+
if (!res.ok) throw new Error(res.statusText)
124+
return res.json()
125+
}
123126

124127
// createInstance takes a defaultProps object and a displayName (both optional)
125-
const AsyncCustomer = createInstance({ promiseFn: loadCustomer }, "AsyncCustomer")
128+
const AsyncPlayer = createInstance({ promiseFn: loadPlayer }, "AsyncPlayer")
126129

127130
const MyComponent = () => (
128-
<AsyncCustomer customerId={1}>
129-
<AsyncCustomer.Fulfilled>{customer => `Hello ${customer.name}`}</AsyncCustomer.Fulfilled>
130-
</AsyncCustomer>
131+
<AsyncPlayer playerId={1}>
132+
<AsyncPlayer.Fulfilled>{player => `Hello ${player.name}`}</AsyncPlayer.Fulfilled>
133+
</AsyncPlayer>
131134
)
132135
```
133136

@@ -138,20 +141,20 @@ Several [helper components](usage.md#helper-components) are available to improve
138141
```jsx
139142
import { useAsync, IfPending, IfFulfilled, IfRejected } from "react-async"
140143

141-
const loadCustomer = async ({ customerId }, { signal }) => {
144+
const loadPlayer = async ({ playerId }, { signal }) => {
142145
// ...
143146
}
144147

145148
const MyComponent = () => {
146-
const state = useAsync({ promiseFn: loadCustomer, customerId: 1 })
149+
const state = useAsync({ promiseFn: loadPlayer, playerId: 1 })
147150
return (
148151
<>
149152
<IfPending state={state}>Loading...</IfPending>
150153
<IfRejected state={state}>{error => `Something went wrong: ${error.message}`}</IfRejected>
151154
<IfFulfilled state={state}>
152155
{data => (
153156
<div>
154-
<strong>Loaded some data:</strong>
157+
<strong>Player data:</strong>
155158
<pre>{JSON.stringify(data, null, 2)}</pre>
156159
</div>
157160
)}
@@ -168,18 +171,19 @@ Each of the helper components are also available as static properties of `<Async
168171
```jsx
169172
import Async from "react-async"
170173

171-
const loadCustomer = ({ customerId }, { signal }) =>
172-
fetch(`/api/customers/${customerId}`, { signal })
173-
.then(res => (res.ok ? res : Promise.reject(res)))
174-
.then(res => res.json())
174+
const loadPlayer = async ({ playerId }, { signal }) => {
175+
const res = await fetch(`/api/players/${playerId}`, { signal })
176+
if (!res.ok) throw new Error(res.statusText)
177+
return res.json()
178+
}
175179

176180
const MyComponent = () => (
177-
<Async promiseFn={loadCustomer} customerId={1}>
181+
<Async promiseFn={loadPlayer} playerId={1}>
178182
<Async.Pending>Loading...</Async.Pending>
179183
<Async.Fulfilled>
180184
{data => (
181185
<div>
182-
<strong>Loaded some data:</strong>
186+
<strong>Player data:</strong>
183187
<pre>{JSON.stringify(data, null, 2)}</pre>
184188
</div>
185189
)}

0 commit comments

Comments
 (0)
Please sign in to comment.