I'm trying to measure a React DOM node on the window resize event. I've used the example on the React hooks-faq, but it only happens for the first render. If I add a useEffect
to listen for the resize, the callback doesn't get called?
function MeasureExample() {
const [height, setHeight] = useState(0);
const measuredRef = useCallback(node => {
if (node !== null) {
setHeight(node.getBoundingClientRect().height);
}
}, []);
// Adding this effect doesn't re-calculate the height???
useEffect(() => {
window.addEventListener("resize", measuredRef);
return (): void => {
window.removeEventListener("resize", measuredRef);
};
}, [height])
return (
<>
<h1 ref={measuredRef}>Hello, world</h1>
<h2>The above header is {Math.round(height)}px tall</h2>
</>
);
}
here is another solution.
function MeasureExample() {
const [height, setHeight] = useState(0);
const [node, setNode] = useState(null);
const measuredRef = useCallback(node => {
if (node !== null) {
setNode(node);
}
}, []);
useLayoutEffect(() => {
if(node){
const measure = () => {
setSize(node.getBoundingClientRect().height);
}
window.addEventListener("resize", measure );
return () => {
window.removeEventListener("resize", measure );
};
}
}, [node])
return (
<>
<h1 ref={measuredRef}>Hello, world</h1>
<h2>The above header is {Math.round(height)}px tall</h2>
</>
);
}
Personally I would extract out the part of listening for resize events. The advantage is it can be used again for something else.
Because resizing is async
, one trick with React is to make the children into a functional return instead of just returning JSX. You can then make your CaptureResize component call the function instead to get it's JSX, and at the same time pass the size to this function.
Below is an example..
const {useLayoutEffect, useRef, useState} = React;
function CaptureResize(props) {
const {captureRef} = props;
function updateSize() {
setSize(captureRef.current.getBoundingClientRect());
}
useLayoutEffect(() => {
updateSize();
window.addEventListener("resize", updateSize);
return () =>
window.removeEventListener("resize", updateSize);
}, []);
const [size, setSize] = useState({});
return props.children(size)
}
function Test() {
const c = useRef(null);
return <CaptureResize captureRef={c}>
{(size) => <React.Fragment>
<h1 ref={c}>Header 1, Resize window to make this go onto diffrent no. of lines</h1>
<div>height of header = {size.height}px</div>
</React.Fragment>}
</CaptureResize>;
}
ReactDOM.render(<React.Fragment>
<Test/>
</React.Fragment>, document.querySelector('#mount'));
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<div id="mount"></div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With