I have used useRef hook for my scroll function, I have set HTMLDivElement type but I am getting Property 'clientHeight, scrollHeight, scrollTop' does not exist on type 'HTMLDivElement | null',
If I hover on useRef it says (alias) useRef<HTMLDivElement>(initialValue: HTMLDivElement | null): RefObject<HTMLDivElement> (+2 overloads) import useRef
const scrollRef = useRef<HTMLDivElement>(null);
const getActivityFeedOnScroll = () => {
const {scrollTop, scrollHeight, clientHeight} = scrollRef?.current;
scrollTop + clientHeight === scrollHeight && fetchNextPage();
};
If I do like it this way, the type is not giving me the error, but I want to replace the if conditions with null guard, to make the code cleaner.
+ const getActivityFeedOnScroll = () => {
+ if (!scrollRef.current) return;
+ const {scrollTop, scrollHeight, clientHeight} = scrollRef.current;
+ if (scrollTop + clientHeight === scrollHeight) fetchNextPage();
+ };