From 2f2e572498bc8d472ca8056ea78c355e4c637b41 Mon Sep 17 00:00:00 2001 From: Sal Olivares Date: Wed, 30 Oct 2019 20:51:33 -0700 Subject: [PATCH] alternate solution: set lastOptions.current on watchfn --- packages/react-async/src/useAsync.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/react-async/src/useAsync.js b/packages/react-async/src/useAsync.js index c80ddaa1..456cd5e6 100644 --- a/packages/react-async/src/useAsync.js +++ b/packages/react-async/src/useAsync.js @@ -97,11 +97,11 @@ const useAsync = (arg1, arg2) => { .then(handleResolve(counter.current)) .catch(handleReject(counter.current)) } else if (promiseFn && !isPreInitialized) { - start(() => promiseFn(options, abortController.current)) + start(() => promiseFn(lastOptions.current, abortController.current)) .then(handleResolve(counter.current)) .catch(handleReject(counter.current)) } - }, [initialValue, promise, promiseFn, start, handleResolve, handleReject, options]) + }, [start, promise, promiseFn, initialValue, handleResolve, handleReject]) const { deferFn } = options const run = useCallback( @@ -132,7 +132,10 @@ const useAsync = (arg1, arg2) => { /* eslint-disable react-hooks/exhaustive-deps */ const { watch, watchFn } = options useEffect(() => { - if (watchFn && lastOptions.current && watchFn(options, lastOptions.current)) load() + if (watchFn && lastOptions.current && watchFn(options, lastOptions.current)) { + lastOptions.current = options + load() + } }) useEffect(() => { lastOptions.current = options