From 037c95e5e8cbc30d257eb29e23ea92502a98b1b5 Mon Sep 17 00:00:00 2001 From: Jacek Pudysz Date: Wed, 25 Oct 2023 16:44:51 +0200 Subject: [PATCH] feat: add support for server in useDimensions --- src/hooks/useDimensions.web.ts | 8 ++++++++ src/utils/common.ts | 1 + src/utils/index.ts | 1 + 3 files changed, 10 insertions(+) diff --git a/src/hooks/useDimensions.web.ts b/src/hooks/useDimensions.web.ts index c8b02dfe..7a023e77 100644 --- a/src/hooks/useDimensions.web.ts +++ b/src/hooks/useDimensions.web.ts @@ -1,7 +1,15 @@ import { useEffect, useRef, useState } from 'react' import type { ScreenSize } from '../types' +import { isServer } from '../utils' export const useDimensions = (): ScreenSize => { + if (isServer()) { + return { + width: 0, + height: 0 + } + } + const timerRef = useRef>() const [screenSize, setScreenSize] = useState({ width: window.innerWidth, diff --git a/src/utils/common.ts b/src/utils/common.ts index b3b6534f..2421e04f 100644 --- a/src/utils/common.ts +++ b/src/utils/common.ts @@ -9,3 +9,4 @@ export const warn = (message: string) => { } export const isWeb = () => Platform.OS === 'web' +export const isServer = () => typeof window === 'undefined' diff --git a/src/utils/index.ts b/src/utils/index.ts index 0cff90a2..abd69664 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -2,6 +2,7 @@ export { normalizeStyles } from './normalizeStyles' export * from './normalizer' export { getBreakpointFromScreenWidth, sortAndValidateBreakpoints, getValueForBreakpoint } from './breakpoints' export { proxifyFunction, parseStyle } from './styles' +export { isServer } from './common' export { extractValues, getKeyForCustomMediaQuery,