Skip to content

Commit

Permalink
replace useLayoutEffect by useIsomorphicLayoutEffect to avoid ssr war…
Browse files Browse the repository at this point in the history
…nings
  • Loading branch information
bmvantunes committed Nov 6, 2019
1 parent 7b46a13 commit 7106961
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/use-swr.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
useState,
useEffect,
useLayoutEffect,
useRef,
useContext,
Expand Down Expand Up @@ -286,8 +287,13 @@ function useSWR<Data = any, Error = any>(
[key]
)

// mounted
useLayoutEffect(() => {
// React currently throws a warning when using useLayoutEffect on the server.
// To get around it, we can conditionally useEffect on the server (no-op) and
// useLayoutEffect in the browser.
const useIsomorphicLayoutEffect = IS_SERVER ? useLayoutEffect : useEffect

// mounted (client side rendering)
useIsomorphicLayoutEffect(() => {
if (!key) return undefined

// after `key` updates, we need to mark it as mounted
Expand Down

0 comments on commit 7106961

Please sign in to comment.