Skip to content

Commit

Permalink
Fix suspense (#777)
Browse files Browse the repository at this point in the history
* fix #494

* add comment

* rename to initialMountedRef
  • Loading branch information
promer94 authored Nov 17, 2020
1 parent db6b62f commit 7fe001c
Showing 1 changed file with 10 additions and 1 deletion.
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

0 comments on commit 7fe001c

Please sign in to comment.