I have a react app that is purely built with functional components. I want to get the height of an element after it has been rendered on screen to change size of it's parent accordingly.
There are solutions for class based apps but I couldn't find any for functional components.
i tried this solution from answers but it is not running the useEffect hook
here is extracted code from my component
import React, {useRef, useEffect} from 'react';
import {
MenuContainer,
GrayBox,
Wrapper
} from '../LayersMenu/LayerMenu.styles';
export const Component = ({ category, showStatistics }) => {
const grayBoxRef= useRef(null);
const wrapperRef= useRef(null);
const testRef= useRef(null);
useEffect(()=>{
const height = wrapperRef.current.offsetHeight;
console.log(height,'check');
}, [wrapperRef])
useEffect(()=>{
const height = testRef.current.offsetHeight;
console.log(height,'check2');
},[testRef]);
return (
<MenuContainer ref={testRef}>
<GrayBox ref={grayBoxRef}>
<Wrapper ref={wrapperRef} hasAnyAllowedStats={showStatistics}>
</Wrapper>
</GrayBox>
</MenuContainer>
);
};
PS: accepted answer didn't work for my answer but it works for the project he added in answer works so i guess there is something wrong with my project structure or something not entirely sure.
Thanks alot for answer