我的 react 中有一个用于滚动的事件处理程序,我试图使其成为被动的。但它不起作用,我不断在控制台中收到此警告:
Consider marking event handler as 'passive' to make the page more responive.
这是我阅读该事件的地方:
bindEventListeners()
{
EventContainer.readWindowEvent('scroll', this.onScroll);
}
这是我的 EventContainer.js 文件中的 readWindowEvent
函数:
static readWindowEvent(slug, callback, passive = true)
{
let result = null;
let allowed = this.getAllowedWindowEventListeners().indexOf(slug) != -1;
if (!allowed) {
result = console.error(
'You cannot register ' + slug + ' on the window. Allowed events: ' +
this.getAllowedWindowEventListeners().join(', ')
)
}
if (allowed) {
result = window.addEventListener(
slug,
callback,
{passive: passive}
);
}
return result;
}
在这一行:
{passive: passive}
我在编辑器中收到以下错误:
Argument type {passive: boolean} is not assignable to parameter type boolean
有人知道我在这里做错了什么吗?
感谢任何帮助!
最佳答案
也许现在回答有点晚了,但我也遇到了同样的问题。解决方法是在传入的对象上使用 any
类型:
let passiveObject: any = { passive: true }
obj.addEventListener(evt, func, passiveObject)
希望这对将来的人有帮助!
关于javascript - 对滚动使用react被动事件处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39617508/