From 38af6355d8209af4dd5f1025db112dfdaada4cc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Berg=C3=A9?= Date: Sat, 23 Nov 2019 19:25:57 +0100 Subject: [PATCH] fix: avoid SSR warning on Text --- packages/shared/core/Text.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/shared/core/Text.js b/packages/shared/core/Text.js index 6cca260b9..d39e64a04 100644 --- a/packages/shared/core/Text.js +++ b/packages/shared/core/Text.js @@ -17,6 +17,14 @@ function ellipsis({ lines = Infinity }) { ` } +const isServer = typeof window === 'undefined' + +function useUniversalLayoutEffect(callback, deps) { + return isServer + ? React.useEffect(callback, deps) + : React.useLayoutEffect(callback, deps) +} + export const TEXT_VARIANTS = [ 'h1', 'h2', @@ -40,7 +48,7 @@ export const Text = createComponent({ const As = asProp || (variantConfig && variantConfig.defaultAs) || 'span' const localRef = React.useRef() const [height, setHeight] = React.useState(null) - React.useLayoutEffect(() => { + useUniversalLayoutEffect(() => { if (!window.getComputedStyle || lines === Infinity) return const style = window.getComputedStyle(localRef.current) const lineHeight = parseInt(style.lineHeight, 10)