Type for React useRef targeting a scroll

Issue

I have a useRef targeting a div to access the scroll property. What type will be it for a scroll.

code:

const scrollRef = useRef<any>(null);

const onClickScroll = (scrollOffset: any) => {
    scrollRef.current.scrollLeft += scrollOffset;
  };

//some code

return(

<div className={styles.book__container} ref={scrollRef}>
//some code
</div>
)

I tried using but doesn’t work.

Solution

try

const scrollRef = useRef<HTMLDivElement>(null);

If you don’t know what is the type, you can hover the ref

enter image description here

Answered By – David Yappeter

Answer Checked By – Marilyn (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.