Skip to content

Commit 4e4d1d7

Browse files
committed
Add support for shorthand useState.
1 parent 3c6a75b commit 4e4d1d7

File tree

2 files changed

+34
-16
lines changed

2 files changed

+34
-16
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,15 @@ const MyComponent = () => {
9393
}
9494
```
9595

96+
Or using the shorthand version:
97+
98+
```js
99+
const MyComponent = () => {
100+
const { data, error, isLoading } = useAsync(loadJson)
101+
// ...
102+
}
103+
```
104+
96105
Using render props for ultimate flexibility:
97106

98107
```js
@@ -194,6 +203,14 @@ Similarly, this allows you to set default `onResolve` and `onReject` callbacks.
194203
- `setData` {Function} sets `data` to the passed value, unsets `error` and cancels any pending promise
195204
- `setError` {Function} sets `error` to the passed value and cancels any pending promise
196205

206+
### `useState`
207+
208+
The `useState` hook accepts an object with the same props as `<Async>`. Alternatively you can use the shorthand syntax:
209+
210+
```js
211+
useState(promiseFn, initialValue)
212+
```
213+
197214
## Examples
198215

199216
### Basic data fetching with loading indicator, error state and retry

src/useAsync.js

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import { useState, useEffect, useMemo } from "react"
22

3-
const useAsync = props => {
3+
const useAsync = (opts, init) => {
44
let counter = 0
55
let isMounted = false
66
let lastArgs = undefined
77

8-
const initialError = props.initialValue instanceof Error ? props.initialValue : undefined
9-
const initialData = initialError ? undefined : props.initialValue
10-
const [data, setData] = useState(initialData)
11-
const [error, setError] = useState(initialError)
12-
const [startedAt, setStartedAt] = useState(props.promiseFn ? new Date() : undefined)
13-
const [finishedAt, setFinishedAt] = useState(props.initialValue ? new Date() : undefined)
8+
const options = typeof opts === "function" ? { promiseFn: opts, initialValue: init } : opts
9+
const { promiseFn, deferFn, initialValue, onResolve, onReject, watch } = options
10+
11+
const [data, setData] = useState(initialValue instanceof Error ? undefined : initialValue)
12+
const [error, setError] = useState(initialValue instanceof Error ? initialValue : undefined)
13+
const [startedAt, setStartedAt] = useState(promiseFn ? new Date() : undefined)
14+
const [finishedAt, setFinishedAt] = useState(initialValue ? new Date() : undefined)
1415

1516
const cancel = () => {
1617
counter++
@@ -44,21 +45,21 @@ const useAsync = props => {
4445
return error
4546
}
4647

47-
const onResolve = count => data => count === counter && handleData(data, props.onResolve)
48-
const onReject = count => error => count === counter && handleError(error, props.onReject)
48+
const handleResolve = count => data => count === counter && handleData(data, onResolve)
49+
const handleReject = count => error => count === counter && handleError(error, onReject)
4950

5051
const load = () => {
51-
if (props.promiseFn) {
52+
if (promiseFn) {
5253
start()
53-
props.promiseFn(props).then(onResolve(counter), onReject(counter))
54+
promiseFn(options).then(handleResolve(counter), handleReject(counter))
5455
}
5556
}
5657

5758
const run = (...args) => {
58-
if (props.deferFn) {
59+
if (deferFn) {
5960
lastArgs = args
6061
start()
61-
return props.deferFn(...args, props).then(onResolve(counter), onReject(counter))
62+
return deferFn(...args, options).then(handleResolve(counter), handleReject(counter))
6263
}
6364
}
6465

@@ -69,7 +70,7 @@ const useAsync = props => {
6970
return () => (isMounted = false)
7071
}, [])
7172

72-
useEffect(load, [props.promiseFn, props.watch])
73+
useEffect(load, [promiseFn, watch])
7374

7475
return useMemo(
7576
() => ({
@@ -78,12 +79,12 @@ const useAsync = props => {
7879
finishedAt,
7980
data,
8081
error,
82+
initialValue,
8183
cancel,
8284
run,
8385
reload,
8486
setData: handleData,
85-
setError: handleError,
86-
initialValue: props.initialValue
87+
setError: handleError
8788
}),
8889
[data, error, startedAt, finishedAt]
8990
)

0 commit comments

Comments
 (0)