I'm storing a reference to an image item using: const renderedImageRef = useRef(). The ref is then assigned in render() function using:
<img ref={renderedImageRef} src=... />
In another JSX item below, I try to access renderedImageRef.current.clientHeight using:
<div style={{top:`${renderedImageRef.current.clientHeight}px`}}>
Hello world
</div>
But this produces an error in the console:
Uncaught TypeError: Cannot read properties of undefined (reading 'clientHeight')
Strangely enough, if I try to access renderedImageRef.current.clientHeight from inside a useEffect hook, it displays the height correctly:
useEffect(() => {
if(renderedImageRef !== null) {
console.log(renderedImageRef)
}
}, [renderedImageRef])
Why am I getting the console error?