Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): avoid occasional "ResizeObserver loop" error #1466

Merged
merged 1 commit into from
May 22, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 17 additions & 2 deletions packages/core/src/hooks/useMeasure.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,35 @@ import ResizeObserver from 'resize-observer-polyfill'

export const useMeasure = () => {
const measureRef = useRef(null)
const animationFrameId = useRef(null)
const [bounds, setBounds] = useState({
left: 0,
top: 0,
width: 0,
height: 0,
})
const [observer] = useState(() => new ResizeObserver(([entry]) => setBounds(entry.contentRect)))
const [observer] = useState(
() =>
new ResizeObserver(([entry]) => {
// wrap this call in requestAnimationFrame to avoid "Resize Observer loop limit exceeded"
// error in certain situations
animationFrameId.current = requestAnimationFrame(() => {
setBounds(entry.contentRect)
})
})
)

useEffect(() => {
if (measureRef.current) {
observer.observe(measureRef.current)
}

return () => observer.disconnect()
return () => {
if (animationFrameId.current) {
cancelAnimationFrame(animationFrameId.current)
}
observer.disconnect()
}
}, [])

return [measureRef, bounds]
Expand Down