How to Handle Scrollbar event in a Table in React

1.4k views Asked by At

I am using React Bootstrap Table and I want an event on the Horizontal Scrolling This is what I have done and it does not seems to work

useEffect(() => {
    let control = document.querySelector('.react-bootstrap-table');
    control.addEventListener('scroll', handleScroll, { passive: true })
  }, []);


  function handleScroll() {
    console.log("hello")
  }

CSS

.react-bootstrap-table{
    overflow-x: auto;
}
1

There are 1 answers

1
deowk On BEST ANSWER

You need to add the event listener to window and not the element:

useEffect(() => {
    window.addEventListener('scroll', handleScroll, { passive: true })
  }, []);


  function handleScroll(e) {
    if (e.currentTarget.className === 'react-bootstrap-table') {
       console.log("hello")
    }
  }