Remove Event Listener On Unmount React

后端 未结 4 1491
眼角桃花
眼角桃花 2020-12-02 22:16

I had higher order component in react like this:

export default function (InnerComponent) {
    class InfiniteScrolling extends React.Component {

        co         


        
相关标签:
4条回答
  • 2020-12-02 22:53
          componentDidMount() {
                window.addEventListener('scroll', this.onScroll, false);
            }
    
            componentWillUnmount() {
                window.removeEventListener('scroll', this.onScroll, false);
            }
            // use arrow function instead
            onScroll = () => { 
                if ((window.innerHeight + window.scrollY) >= (document.body.offsetHeight - 50)) {
                    const { scrollFunc } = this.props;
                    scrollFunc();
                }
            }
    

    or you can use Arrow functions , to solve .bind(this) problems it worked form just fine.

    0 讨论(0)
  • 2020-12-02 22:54

    .bind always creates a new function so you need to do like below, so it adds and removes the same function.

        constructor(props){
            super(props);
            this.onScroll = this.onScroll.bind(this); //bind function once
        }
    
        componentDidMount() {
            window.addEventListener('scroll', this.onScroll, false);
        }
    
        componentWillUnmount() {
            // you need to unbind the same listener that was binded.
            window.removeEventListener('scroll', this.onScroll, false);
        }
    
    0 讨论(0)
  • 2020-12-02 23:12

    I know it's a little bit late, but I just encounter this issue and wanted to share with you my solution, looking forward to any feedback. this solution includes react hooks. I hope you like

    // Declare a static listener.
    const eventListeners = useRef();
    
    // now let's create our scroll Handler
    const scrollHandler = useCallback(() => {...},[]);
    
    useEffect(() => {
       // Here will be removing the static listener and then updated it for 
       // our new one since the first time will be empty it won't do anything.
        window.removeEventListener('scroll', eventListeners.current, true);
    
        // Then will set our current scroll handler to our static listener
        eventListeners.current = scrollHandler;
    
        // Here will be adding the static listener so we can keep the reference
        // and remove it later on
        window.addEventListener('scroll', eventListeners.current, true);
    },[scrollHandler]);
    
    
    0 讨论(0)
  • 2020-12-02 23:13

    A working version for my project with Arrow Function and no-bind:

    componentDidMount = () => {
      window.addEventListener("wheel", this.onScroll, false);
    };
    
    componentWillUnmount() {
      window.removeEventListener("wheel", this.onScroll, false);
    }
    
    onScroll = (e) => {
      const item = this.refs.myElement;
      if (e.deltaY > 0) item.scrollLeft += 200;
      else item.scrollLeft -= 200;
    };
    
    0 讨论(0)
提交回复
热议问题