Skip to content

Commit 73460e1

Browse files
committed
fix: 🐛 backward compatibility return dispatchAsync
1 parent f32753f commit 73460e1

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/useDispatchAsync.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ interface ResultUnknown {
2929
status: 'unknown'
3030
}
3131

32+
type CompatActionResult<R> = (action: Action) => Promise<DispatchAsyncResult<R>>
33+
3234
export type UseDispatchAsync<R = unknown> =
3335
| ResultLoading
3436
| ResultSuccess<R>
@@ -47,12 +49,17 @@ type InnerPromiseType = Promise<boolean | DispatchAsyncResult>
4749

4850
// the hook
4951
export function useDispatchAsync<R = any>(
50-
actionFunction: (...args: any[]) => Action & { payload: any },
52+
actionFunction?: (...args: any[]) => Action & { payload: any },
5153
deps: any[] = [],
5254
options: Options = { timeoutInMilliseconds: 15000 }, // wait 15s
53-
): UseDispatchAsync<R> {
55+
): UseDispatchAsync<R> | CompatActionResult<R> {
5456
const dispatch = useDispatch()
5557

58+
// 👉 backward compatibility
59+
if (!actionFunction) {
60+
return (action: Action) => dispatchAsync<R>(dispatch, action)
61+
}
62+
5663
// 👉 Better flow with informative & useful return
5764
const [result, setResult] = useState<R | undefined>(undefined)
5865
const [error, setError] = useState<Error | undefined>(undefined)

0 commit comments

Comments
 (0)