Created
April 18, 2019 18:16
-
-
Save stolinski/1fecfb2677e0b2deda9039608fecf5d8 to your computer and use it in GitHub Desktop.
useMeasure - taken from React Spring example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useRef, useState, useEffect } from 'react' | |
import ResizeObserver from 'resize-observer-polyfill' | |
export default function useMeasure() { | |
const ref = useRef() | |
const [bounds, set] = useState({ left: 0, top: 0, width: 0, height: 0 }) | |
const [ro] = useState(() => new ResizeObserver(([entry]) => set(entry.contentRect))) | |
useEffect(() => (ro.observe(ref.current), ro.disconnect), []) | |
return [{ ref }, bounds] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
useEffect
needs to update like this.