I am using react, typescript and tailwind. I want one element's height to match the height of another element. It would be very simple usually but I have a complicated div structure so I can't do it in a normal way. What I have attempted is to calculate the offsetHeight and offsetTopHeight, and set the second element's height this value. The code is below, but I have removed a lot of the other code not related to this to make it easier for y'all to understand.
const [offsetTopHeight, setOffsetTopHeight] = useState(0);
const [offsetHeight, setOffsetHeight] = useState(0);
const [totalHeight, setTotalHeight] = useState(0);
const ref = useRef<HTMLDivElement>(null);
useEffect(() => {
setTimeout(() => {
if (ref.current != null) {
setTopHeight(ref.current.offsetTop);
setOffHeight(ref.current.offsetHeight);
setTotalHeight(ref.current.offsetTop + ref.current.offsetHeight);
console.log(
"total height: ",
ref.current.offsetTop + ref.current.offsetHeight
);
console.log("offset height: ", ref.current.offsetHeight);
console.log("offset top: ", ref.current.offsetTop);
}
}, 300);
}, []);
return (
<div id="this-div-copies-height" className={`h-[${totalHeight.toString()}px]`}></div>
<div id="this-div-sets-height" ref={ref}></div>
);
I set a timeout cause I read that it would delay the calculation of the height till after the element rendered. Even without the timeout the code doesn't work though. Offset height finds the height of the element, and offset top height finds the distance between the top of the screen and the top of the element. I have added these to get the distance from the top of the screen to the bottom of the element. Any help would be appreciated, thanks!