在reactjs(JSX)中使用eventlistener

在reactjs(JSX)中使用eventlistener,reactjs,react-jsx,Reactjs,React Jsx,我正在努力学习Reactjs,但我无法解决它。 我想将sticky header类添加到使用Reactjs呈现的表中,但使用此代码/伪代码我没有获得任何成功 handleScroll: function(e){ var header = this.root.querySelector('.header'); var origOffsetY = header.offsetTop; window.scrollY >= origOffsetY ? header.addClass('sti

我正在努力学习Reactjs,但我无法解决它。 我想将sticky header类添加到使用Reactjs呈现的表中,但使用此代码/伪代码我没有获得任何成功

handleScroll: function(e){
  var header = this.root.querySelector('.header');
var origOffsetY = header.offsetTop;
  window.scrollY >= origOffsetY ? header.addClass('sticky'): header.removeClass('sticky');
},

componentDidMount : function(){
window.addEventListener('handleScroll',this.handleScroll);
},
我正在尝试使用此Javascript函数:

<script>
var header = document.querySelector('.header');
var origOffsetY = header.offsetTop;

function onScroll(e) {
  window.scrollY >= origOffsetY ? header.classList.add('sticky') :
                                  header.classList.remove('sticky');
}

document.addEventListener('scroll', onScroll);
</script>

var header=document.querySelector('.header');
var origOffsetY=header.offsetTop;
函数onScroll(e){
window.scrollY>=origOffsetY?header.classList.add('sticky'):
header.classList.remove('sticky');
}
文件。添加的文件列表(“滚动”,在滚动条上);
但我没有取得任何成功。在Reactjs中实现javascript函数有没有更简单的方法

排队

window.addEventListener('handleScroll', this.handleScroll);
我猜你的意思是:

window.addEventListener('scroll', this.handleScroll);
排队

window.addEventListener('handleScroll', this.handleScroll);
我猜你的意思是:

window.addEventListener('scroll', this.handleScroll);
排队

window.addEventListener('handleScroll', this.handleScroll);
我猜你的意思是:

window.addEventListener('scroll', this.handleScroll);
排队

window.addEventListener('handleScroll', this.handleScroll);
我猜你的意思是:

window.addEventListener('scroll', this.handleScroll);

是的,我在控制台中也看到了这个错误:无法读取未定义的属性'querySelector'。this.root是什么?当您试图在
handleScroll
中访问它时,它没有定义。我在向下滚动时看到此错误,因此我认为该功能在滚动期间触发,但我可能必须找出此对象。一定是语法错误。感谢您的回复。很抱歉打扰大家,但我所要做的只是滚动香草JS函数/*********************/handleScroll:function(e){var header=document.querySelector('.header');var origOffsetY=header.offsetTop;window.scrollY>=origOffsetY?header.classList.add('sticky')):header.classList.remove('sticky');},componentDidMount:function(){window.addEventListener('scroll',this.handleScroll);},/******************************************/是的,我还在控制台中看到这个错误:无法读取未定义的属性'querySelector'。this.root是什么?当您试图在
handleScroll
中访问它时,它没有定义。我在向下滚动时看到此错误,因此我认为该功能在滚动期间触发,但我可能必须找出此对象。一定是语法错误。感谢您的回复。很抱歉打扰大家,但我所要做的只是滚动香草JS函数/*********************/handleScroll:function(e){var header=document.querySelector('.header');var origOffsetY=header.offsetTop;window.scrollY>=origOffsetY?header.classList.add('sticky')):header.classList.remove('sticky');},componentDidMount:function(){window.addEventListener('scroll',this.handleScroll);},/******************************************/是的,我还在控制台中看到这个错误:无法读取未定义的属性'querySelector'。this.root是什么?当您试图在
handleScroll
中访问它时,它没有定义。我在向下滚动时看到此错误,因此我认为该功能在滚动期间触发,但我可能必须找出此对象。一定是语法错误。感谢您的回复。很抱歉打扰大家,但我所要做的只是滚动香草JS函数/*********************/handleScroll:function(e){var header=document.querySelector('.header');var origOffsetY=header.offsetTop;window.scrollY>=origOffsetY?header.classList.add('sticky')):header.classList.remove('sticky');},componentDidMount:function(){window.addEventListener('scroll',this.handleScroll);},/******************************************/是的,我还在控制台中看到这个错误:无法读取未定义的属性'querySelector'。this.root是什么?当您试图在
handleScroll
中访问它时,它没有定义。我在向下滚动时看到此错误,因此我认为该功能在滚动期间触发,但我可能必须找出此对象。一定是语法错误。感谢您的回复。很抱歉打扰大家,但我所要做的只是滚动香草JS函数/*********************/handleScroll:function(e){var header=document.querySelector('.header');var origOffsetY=header.offsetTop;window.scrollY>=origOffsetY?header.classList.add('sticky')):header.classList.remove('sticky');},componentDidMount:function(){window.addEventListener('scroll',this.handleScroll);}/**********************/