@@ -9,20 +9,21 @@ The `useAsync` hook \(available [from React v16.8.0](https://reactjs.org/hooks)\
9
9
``` jsx
10
10
import { useAsync } from " react-async"
11
11
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 )
15
16
return res .json ()
16
17
}
17
18
18
19
const MyComponent = () => {
19
- const { data , error , isPending } = useAsync ({ promiseFn: loadCustomer, customerId : 1 })
20
+ const { data , error , isPending } = useAsync ({ promiseFn: loadPlayer, playerId : 1 })
20
21
if (isPending) return " Loading..."
21
22
if (error) return ` Something went wrong: ${ error .message } `
22
23
if (data)
23
24
return (
24
25
< div>
25
- < strong> Loaded some data: < / strong>
26
+ < strong> Player data: < / strong>
26
27
< pre> {JSON .stringify (data, null , 2 )}< / pre>
27
28
< / div>
28
29
)
@@ -36,7 +37,7 @@ Or using the shorthand version:
36
37
37
38
``` jsx
38
39
const MyComponent = () => {
39
- const { data , error , isPending } = useAsync (loadCustomer , options)
40
+ const { data , error , isPending } = useAsync (loadPlayer , options)
40
41
// ...
41
42
}
42
43
```
@@ -84,20 +85,21 @@ The classic interface to React Async. Simply use `<Async>` directly in your JSX
84
85
import Async from " react-async"
85
86
86
87
// 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
+ }
91
93
92
94
const MyComponent = () => (
93
- < Async promiseFn= {loadCustomer} customerId = {1 }>
95
+ < Async promiseFn= {loadPlayer} playerId = {1 }>
94
96
{({ data, error, isPending }) => {
95
97
if (isPending) return " Loading..."
96
98
if (error) return ` Something went wrong: ${ error .message } `
97
99
if (data)
98
100
return (
99
101
< div>
100
- < strong> Loaded some data: < / strong>
102
+ < strong> Player data: < / strong>
101
103
< pre> {JSON .stringify (data, null , 2 )}< / pre>
102
104
< / div>
103
105
)
@@ -116,18 +118,19 @@ You can also create your own component instances, allowing you to preconfigure t
116
118
``` jsx
117
119
import { createInstance } from " react-async"
118
120
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
+ }
123
126
124
127
// createInstance takes a defaultProps object and a displayName (both optional)
125
- const AsyncCustomer = createInstance ({ promiseFn: loadCustomer }, " AsyncCustomer " )
128
+ const AsyncPlayer = createInstance ({ promiseFn: loadPlayer }, " AsyncPlayer " )
126
129
127
130
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 >
131
134
)
132
135
```
133
136
@@ -138,20 +141,20 @@ Several [helper components](usage.md#helper-components) are available to improve
138
141
``` jsx
139
142
import { useAsync , IfPending , IfFulfilled , IfRejected } from " react-async"
140
143
141
- const loadCustomer = async ({ customerId }, { signal }) => {
144
+ const loadPlayer = async ({ playerId }, { signal }) => {
142
145
// ...
143
146
}
144
147
145
148
const MyComponent = () => {
146
- const state = useAsync ({ promiseFn: loadCustomer, customerId : 1 })
149
+ const state = useAsync ({ promiseFn: loadPlayer, playerId : 1 })
147
150
return (
148
151
<>
149
152
< IfPending state= {state}> Loading... < / IfPending>
150
153
< IfRejected state= {state}> {error => ` Something went wrong: ${ error .message } ` }< / IfRejected>
151
154
< IfFulfilled state= {state}>
152
155
{data => (
153
156
< div>
154
- < strong> Loaded some data: < / strong>
157
+ < strong> Player data: < / strong>
155
158
< pre> {JSON .stringify (data, null , 2 )}< / pre>
156
159
< / div>
157
160
)}
@@ -168,18 +171,19 @@ Each of the helper components are also available as static properties of `<Async
168
171
``` jsx
169
172
import Async from " react-async"
170
173
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
+ }
175
179
176
180
const MyComponent = () => (
177
- < Async promiseFn= {loadCustomer} customerId = {1 }>
181
+ < Async promiseFn= {loadPlayer} playerId = {1 }>
178
182
< Async .Pending > Loading... < / Async .Pending >
179
183
< Async .Fulfilled >
180
184
{data => (
181
185
< div>
182
- < strong> Loaded some data: < / strong>
186
+ < strong> Player data: < / strong>
183
187
< pre> {JSON .stringify (data, null , 2 )}< / pre>
184
188
< / div>
185
189
)}
0 commit comments