Last active
November 9, 2019 04:59
-
-
Save njdancer/b0f944a9c2b80b919c8419e585c79940 to your computer and use it in GitHub Desktop.
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
interface Measurements { | |
x: number | |
y: number | |
width: number | |
height: number | |
pageX: number | |
pageY: number | |
} | |
export const useMeasure: () => [RefObject<View>, Measurements] = () => { | |
const [x, setX] = useState(0) | |
const [y, setY] = useState(0) | |
const [width, setWidth] = useState(0) | |
const [height, setHeight] = useState(0) | |
const [pageX, setPageX] = useState(0) | |
const [pageY, setPageY] = useState(0) | |
const ref = useRef<View>(null) | |
useEffect(() => { | |
if (ref.current != null) { | |
ref.current.measure((x, y, width, height, pageX, pageY) => { | |
setX(x) | |
setY(y) | |
setWidth(width) | |
setHeight(height) | |
setPageX(pageX) | |
setPageY(pageY) | |
}) | |
} | |
}) | |
return [ref, { height, pageX, pageY, width, x, y }] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment