Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What will be Scroll Event Type in TypeScript and Reactjs

I know we can use event listener and work with ScrollEvent.

But suppose following method:

handleScroll =(event:any):void=>{
const target = event.target;
// further code goes her
}

I want to use event type instead of any so what should be its type?

like image 883
Shashi Singh Avatar asked Oct 15 '25 17:10

Shashi Singh


2 Answers

if you aim to catch the mouse-wheel scroll event then you can use this type.

handleScroll = (event: WheelEvent): void =>
like image 121
Veljko Blagojevic Avatar answered Oct 18 '25 10:10

Veljko Blagojevic


If you are not doing this in React then you would use

handleScroll = (event: Event):void => {
  const target = event.target;
  // further code goes here
}

If you using React and the scrolling is being done in a <div> then you would use

handleScroll = (event: React.UIEvent<HTMLDivElement>):void => {
  const target = event.target;
  // further code goes here
}

If the event is occuring on another type of element (and using React) then you should substitute that for <HTMLDivElement>.

As per a comment I wrote below another post here, you should not confuse wheel and scroll events.

like image 21
flyingace Avatar answered Oct 18 '25 12:10

flyingace



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!