Skip to content

Commit

Permalink
fix: enabled effect and drop unecessary refs
Browse files Browse the repository at this point in the history
  • Loading branch information
DorianMaliszewski committed May 31, 2021
1 parent ad5d479 commit a3bc384
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions packages/use-dataloader/src/useDataLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ const useDataLoader = (
const previousDataRef = useRef()
const isMountedRef = useRef(enabled)
const methodRef = useRef(method)
const onSuccessRef = useRef(onSuccess)
const onErrorRef = useRef(onError)

const isLoading = useMemo(() => status === StatusEnum.LOADING, [status])
const isIdle = useMemo(() => status === StatusEnum.IDLE, [status])
Expand All @@ -103,10 +101,10 @@ const useDataLoader = (

dispatch(Actions.createOnSuccess())

await onSuccessRef.current?.(result)
await onSuccess?.(result)
} catch (err) {
dispatch(Actions.createOnError(err))
await onErrorRef.current?.(err)
await onError?.(err)
}
})

Expand All @@ -116,7 +114,7 @@ const useDataLoader = (
if (isIdle) {
handleRequest.current(key)
}
if (pollingInterval && isSuccess) {
if (pollingInterval && (isSuccess || isError)) {
handler = setTimeout(() => handleRequest.current(key), pollingInterval)
}
}
Expand All @@ -125,14 +123,13 @@ const useDataLoader = (
if (handler) clearTimeout(handler)
}
// Why can't put empty array for componentDidMount, componentWillUnmount ??? No array act like componentDidMount and componentDidUpdate
}, [key, pollingInterval, isIdle, isSuccess])
}, [key, pollingInterval, isIdle, isSuccess, isError, enabled])

useLayoutEffect(() => {
methodRef.current = method
}, [method])

useLayoutEffect(() => {
isMountedRef.current = enabled
dispatch(Actions.createReset())
if (key && typeof key === 'string') {
addReloadRef.current?.(key, reloadArgs =>
Expand Down

0 comments on commit a3bc384

Please sign in to comment.