最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What will be Scroll Event Type in TypeScript and Reactjs - Stack Overflow

programmeradmin0浏览0评论

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?

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?

Share Improve this question asked Aug 13, 2020 at 13:49 Shashi SinghShashi Singh 311 gold badge1 silver badge2 bronze badges 1
  • Try inlining your event handler, <Component scroll={event => event.} and see what type inference serves you up ;) – Aluan Haddad Commented Aug 13, 2020 at 13:53
Add a ment  | 

2 Answers 2

Reset to default 4

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

handleScroll = (event: WheelEvent): void =>

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 ment I wrote below another post here, you should not confuse wheel and scroll events.

发布评论

评论列表(0)

  1. 暂无评论