File tree Expand file tree Collapse file tree 1 file changed +9
-2
lines changed Expand file tree Collapse file tree 1 file changed +9
-2
lines changed Original file line number Diff line number Diff line change @@ -29,6 +29,8 @@ interface ResultUnknown {
2929 status : 'unknown'
3030}
3131
32+ type CompatActionResult < R > = ( action : Action ) => Promise < DispatchAsyncResult < R > >
33+
3234export type UseDispatchAsync < R = unknown > =
3335 | ResultLoading
3436 | ResultSuccess < R >
@@ -47,12 +49,17 @@ type InnerPromiseType = Promise<boolean | DispatchAsyncResult>
4749
4850// the hook
4951export 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 )
You can’t perform that action at this time.
0 commit comments