Skip to content

Commit 2763c4a

Browse files
committedJan 12, 2022
fix(core): ignore useMeasure hook if not in a browser context
1 parent f0a6730 commit 2763c4a

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed
 

‎packages/core/src/hooks/useMeasure.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,30 @@ import { useRef, useState, useEffect } from 'react'
22

33
export const useMeasure = () => {
44
const measureRef = useRef(null)
5+
56
const [bounds, setBounds] = useState({
67
left: 0,
78
top: 0,
89
width: 0,
910
height: 0,
1011
})
11-
const [observer] = useState(() => new ResizeObserver(([entry]) => setBounds(entry.contentRect)))
12+
13+
const [observer] = useState(() => {
14+
// Check if window is defined (so if in the browser or in node.js).
15+
const isBrowser = typeof window !== 'undefined'
16+
if (!isBrowser) return null
17+
18+
return new ResizeObserver(([entry]) => setBounds(entry.contentRect))
19+
})
1220

1321
useEffect(() => {
14-
if (measureRef.current) {
22+
if (measureRef.current && observer !== null) {
1523
observer.observe(measureRef.current)
1624
}
1725

18-
return () => observer.disconnect()
26+
return () => {
27+
if (observer !== null) observer.disconnect()
28+
}
1929
}, [])
2030

2131
return [measureRef, bounds]

0 commit comments

Comments
 (0)
Please sign in to comment.