diff --git a/src/index.tsx b/src/index.tsx index f4f2c42..3a09f86 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1,6 +1,7 @@ import React, { useRef, useState, + useEffect, useLayoutEffect, useCallback, useMemo, @@ -81,6 +82,9 @@ function stylesForItem({ const lastKnownHeights = {} as Record; +const useIsomorphicLayoutEffect = + typeof window !== 'undefined' ? useLayoutEffect : useEffect; + export function SnappyReactGrid({ id = 'default', items, @@ -173,7 +177,7 @@ export function SnappyReactGrid({ } }, [updateItemVisibility, onScroll]); - useLayoutEffect(() => { + useIsomorphicLayoutEffect(() => { styleCache.current = {}; updateItemVisibility();