From 88c2cfab1ebfc28cd9fe456e1e63a39187273bca Mon Sep 17 00:00:00 2001 From: edelgarat Date: Sun, 28 Jul 2024 23:03:17 +0300 Subject: [PATCH] update useAsyncFn hook --- package-lock.json | 4 ++-- package.json | 2 +- src/hooks/useAsyncFn.ts | 9 +++------ 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/package-lock.json b/package-lock.json index 0295ccf..0ff539d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@worksolutions/react-utils", - "version": "2.2.5", + "version": "2.2.6", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@worksolutions/react-utils", - "version": "2.2.5", + "version": "2.2.6", "dependencies": { "@popperjs/core": "^2.*", "@worksolutions/utils": "^1.4.3", diff --git a/package.json b/package.json index 945da17..735217d 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@worksolutions/react-utils", "private": false, - "version": "2.2.5", + "version": "2.2.6", "description": "", "types": "dist/esm/index.d.ts", "main": "dist/cjs/index.js", diff --git a/src/hooks/useAsyncFn.ts b/src/hooks/useAsyncFn.ts index 66b9e41..9e70cdc 100644 --- a/src/hooks/useAsyncFn.ts +++ b/src/hooks/useAsyncFn.ts @@ -13,17 +13,14 @@ export type AsyncState = type FunctionReturningPromise = (...args: any[]) => Promise; -const initialStateValue = { loading: false }; export function useAsyncFn>( func: FUNC, deps: DependencyList, - initialState: AsyncState< - FUNC extends FunctionReturningPromise ? RESULT : unknown - > = initialStateValue as any, + initialState?: AsyncState ? RESULT : unknown>, ) { const lastCallId = React.useRef(0); const isMounted = useMountedState(); - const [state, setState] = React.useState(initialState); + const [state, setState] = React.useState(initialState ?? { loading: false }); const stateRef = useSyncToRef(state); const loadingAvailable = React.useRef(true); const enableLoadingAvailable = useCallback(() => (loadingAvailable.current = true), []); @@ -41,7 +38,7 @@ export function useAsyncFn>( }, (error) => { if (isMounted() && callId === lastCallId.current) setState({ error, loading: false }); - return error; + throw error; }, ); // eslint-disable-next-line react-hooks/exhaustive-deps