Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unable to fetch data from useRef

I am trying to get the height of the div from my project. But when I run this I get Property 'offsetHeight' does not exist on type 'never'

This is my code:

const customMenuRef = useRef(null);

useEffect(() => {
    if (customMenuRef) {
        console.log(customMenuRef.current?.offsetHeight);
    }
}, [customMenuRef]);

return (
    <div ref={customMenuRef}>
        test
    </div>
);

If I simply run console.log(customMenuRef.Current) I do get a current object with all the info I need, but I simply can’t reacht hem.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Maybe this can help you

const customMenuRef = useRef<HTMLDivElement>(null);

useEffect(() => {
    if (customMenuRef) {
        console.log(customMenuRef.current?.offsetHeight);
    }
}, [customMenuRef]);

return (
    <div ref={customMenuRef}>
        test
    </div>
);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading