I am trying to create an image (based on src
) and also show its dimensions at the same time.
I tried
const DisplayImage = () => {
const [src,setSrc]=useState('');
return (
<div>
{src===''?'':<img id='my-img' src={image} alt='some text' />}
{document.getElementById('my-img')?.naturalHeight}
</div>);
}
Of course, it's not working (the image does not exist when I try to find its height).
I also tried to do it in a JavaScript way, creating an Image()
and calling document.appendChild
to the document, but it is probably not the right way of doing it in React.
Is there a better way of doing it?