Home > OS >  Unable to fetch data from useRef
Unable to fetch data from useRef

Time:02-17

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.

CodePudding user response:

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>
);
  • Related