From dc1beb131c8d1e0b38a9e3f0b89bf1173e0a21a4 Mon Sep 17 00:00:00 2001 From: Yixuan Xu Date: Thu, 19 May 2022 04:33:47 +0800 Subject: [PATCH] refactor: remove useless ref copy (#1981) --- core/use-swr.ts | 28 +++++++--------------------- 1 file changed, 7 insertions(+), 21 deletions(-) diff --git a/core/use-swr.ts b/core/use-swr.ts index 403b405ea..a9f6032b1 100644 --- a/core/use-swr.ts +++ b/core/use-swr.ts @@ -148,8 +148,7 @@ export const useSWRHandler = ( const cached = useSyncExternalStoreWithSelector( useCallback( (callback: () => void) => - subscribeCache(key, (current: State) => { - stateRef.current = current + subscribeCache(key, () => { callback() }), // eslint-disable-next-line react-hooks/exhaustive-deps @@ -161,7 +160,6 @@ export const useSWRHandler = ( isEqual ) - const stateRef = useRef>(cached) const isInitialMount = !initialMountedRef.current const cachedData = cached.data @@ -209,13 +207,6 @@ export const useSWRHandler = ( const isValidating = cached.isValidating || defaultValidatingState const isLoading = cached.isLoading || defaultValidatingState - const currentState = { - data, - error, - isValidating, - isLoading - } - // The revalidation function is a carefully crafted wrapper of the original // `fetcher`, to correctly handle the many edge cases. const revalidate = useCallback( @@ -368,15 +359,11 @@ export const useSWRHandler = ( } // Deep compare with latest state to avoid extra re-renders. // For local state, compare and assign. - if (!compare(stateRef.current.data, newData)) { - finalState.data = newData - } else { - // `data` and `newData` are deeply equal (serialized value). - // So it should be safe to broadcast the stale data to keep referential equality (===). - finalState.data = stateRef.current.data - // At the end of this function, `broadcastState` invokes the `onStateUpdate` function, - // which takes care of avoiding the re-render. - } + const cacheData = getCache().data + + // Since the compare fn could be custom fn + // cacheData might be different from newData even when compare fn returns True + finalState.data = compare(cacheData, newData) ? cacheData : newData // Trigger the successful callback if it's the original request. if (shouldStartNewRequest) { @@ -462,7 +449,6 @@ export const useSWRHandler = ( useIsomorphicLayoutEffect(() => { fetcherRef.current = fetcher configRef.current = config - stateRef.current = currentState // Handle laggy data updates. If there's cached data of the current key, // it'll be the correct reference. if (!isUndefined(cachedData)) { @@ -550,7 +536,7 @@ export const useSWRHandler = ( // Check if it's OK to execute: // Only revalidate when the page is visible, online and not errored. if ( - !stateRef.current.error && + !getCache().error && (refreshWhenHidden || getConfig().isVisible()) && (refreshWhenOffline || getConfig().isOnline()) ) {