Skip to content

Fix suspense #777

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

Merged
merged 3 commits into from
Nov 17, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/use-swr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,11 @@ function useSWR<Data = any, Error = any>(
shouldUpdateState = true
}
}

if (shouldUpdateState || config.suspense) {
if (unmountedRef.current) return
// if component is unmounted, should skip rerender
// if component is not mounted, should skip rerender
if (unmountedRef.current || !initialMountedRef.current) return
rerender({})
}
}, [])
Expand All @@ -325,6 +328,9 @@ function useSWR<Data = any, Error = any>(
const unmountedRef = useRef(false)
const keyRef = useRef(key)

// check if component is mounted in suspense mode
const initialMountedRef = useRef(false)

// do unmount check for callbacks
const eventsRef = useRef({
emit: (event, ...params) => {
Expand Down Expand Up @@ -536,6 +542,8 @@ function useSWR<Data = any, Error = any>(
// after `key` updates, we need to mark it as mounted
unmountedRef.current = false

initialMountedRef.current = true

// after the component is mounted (hydrated),
// we need to update the data from the cache
// and trigger a revalidation
Expand Down Expand Up @@ -744,6 +752,7 @@ function useSWR<Data = any, Error = any>(
if (!CONCURRENT_PROMISES[key]) {
// trigger revalidate immediately
// to get the promise
// in this revalidate, should not rerender
revalidate()
}

Expand Down