Sai Krishnadas
Sai Krishnadas

Reputation: 3419

Type for React useRef targeting a scroll

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.

Upvotes: 2

Views: 2172

Answers (2)

David Yappeter
David Yappeter

Reputation: 1612

try

const scrollRef = useRef<HTMLDivElement>(null);

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

enter image description here

Upvotes: 4

tenshi
tenshi

Reputation: 26344

HTMLDivElement | null it seems:

const scrollRef = useRef<HTMLDivElement | null>(null);

You might also want to use only HTMLDivElement so you don't have to spam ! everywhere.

Upvotes: 2

Related Questions