Skip to content

Commit 34a6994

Browse files
authored
Merge pull request #31 from micah-redwood/uselayouteffect-ssr-warning
Avoid useLayoutEffect warning during SSR
2 parents 6ecac8e + 4c08701 commit 34a6994

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import {
33
useMemo,
44
useCallback,
55
useRef,
6-
useLayoutEffect
6+
useLayoutEffect,
7+
useEffect,
78
} from 'react';
89

910
function getVpWidth() {
@@ -21,6 +22,12 @@ function getVpHeight() {
2122
) : 0;
2223
}
2324

25+
// Avoid useLayoutEffect warning during SSR
26+
// https://usehooks-ts.com/react-hook/use-isomorphic-layout-effect
27+
function useIsomorphicLayoutEffect() {
28+
typeof window !== 'undefined' ? useLayoutEffect : useEffect;
29+
}
30+
2431
// =============== //
2532
// Shared State //
2633
// =============== //
@@ -204,7 +211,7 @@ export default function useViewportSizes(input) {
204211
}
205212
}, [debounceTimeoutRef, hasher, dimension, state]);
206213

207-
useLayoutEffect(() => {
214+
useIsomorphicLayoutEffect(() => {
208215
resolverMap.set(listener, {
209216
options,
210217
prevHash: state.hash || undefined

0 commit comments

Comments
 (0)