diff --git a/src/index.ts b/src/index.ts index 05c52c9..37774b9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,6 +4,7 @@ import { useCallback, useEffect, useLayoutEffect, + useMemo, useRef, useState, } from 'react'; @@ -238,9 +239,12 @@ const useAsyncInternal = ( const normalizedOptions = normalizeOptions(options); + // Use memoization to produce a stable options object post-normalization. + const memoizedOptions = useMemo(() => normalizedOptions, Object.values(normalizedOptions)); + const [currentParams, setCurrentParams] = useState(null); - const AsyncState = useAsyncState(normalizedOptions); + const AsyncState = useAsyncState(memoizedOptions); const isMounted = useIsMounted(); const CurrentPromise = useCurrentPromise(); @@ -263,7 +267,7 @@ const useAsyncInternal = ( if (shouldHandlePromise(promise)) { AsyncState.setResult(result); } - normalizedOptions.onSuccess(result, { + memoizedOptions.onSuccess(result, { isCurrent: () => CurrentPromise.is(promise), }); }, @@ -271,7 +275,7 @@ const useAsyncInternal = ( if (shouldHandlePromise(promise)) { AsyncState.setError(error); } - normalizedOptions.onError(error, { + memoizedOptions.onError(error, { isCurrent: () => CurrentPromise.is(promise), }); } @@ -291,8 +295,8 @@ const useAsyncInternal = ( const isMounting = !isMounted(); useEffect(() => { const execute = () => getLatestExecuteAsyncOperation()(...params); - isMounting && normalizedOptions.executeOnMount && execute(); - !isMounting && normalizedOptions.executeOnUpdate && execute(); + isMounting && memoizedOptions.executeOnMount && execute(); + !isMounting && memoizedOptions.executeOnUpdate && execute(); }, params); return {