Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove useless ref copy #1981

Merged
merged 1 commit into from May 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 7 additions & 21 deletions core/use-swr.ts
Expand Up @@ -148,8 +148,7 @@ export const useSWRHandler = <Data = any, Error = any>(
const cached = useSyncExternalStoreWithSelector(
useCallback(
(callback: () => void) =>
subscribeCache(key, (current: State<Data, any>) => {
stateRef.current = current
subscribeCache(key, () => {
callback()
}),
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand All @@ -161,7 +160,6 @@ export const useSWRHandler = <Data = any, Error = any>(
isEqual
)

const stateRef = useRef<State<Data, Error>>(cached)
const isInitialMount = !initialMountedRef.current
const cachedData = cached.data

Expand Down Expand Up @@ -209,13 +207,6 @@ export const useSWRHandler = <Data = any, Error = any>(
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(
Expand Down Expand Up @@ -368,15 +359,11 @@ export const useSWRHandler = <Data = any, Error = any>(
}
// 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) {
Expand Down Expand Up @@ -462,7 +449,6 @@ export const useSWRHandler = <Data = any, Error = any>(
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)) {
Expand Down Expand Up @@ -550,7 +536,7 @@ export const useSWRHandler = <Data = any, Error = any>(
// 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())
) {
Expand Down