Stores' change listeners not getting removed on componentWillUnmount?

前端 未结 7 1925
一向
一向 2020-12-29 14:34

I am coding a simple app on reactjs-flux and everything works fine except I am receiving a warning from reactjs telling me that I am calling setState on unmounted components

7条回答
  •  青春惊慌失措
    2020-12-29 15:14

    I decided it so

    class Tooltip extends React.Component {
        constructor (props) {
            super(props);
    
            this.state = {
                 handleOutsideClick: this.handleOutsideClick.bind(this)
            };
        }
    
        componentDidMount () {
             window.addEventListener('click', this.state.handleOutsideClick);
        }
    
        componentWillUnmount () {
             window.removeEventListener('click', this.state.handleOutsideClick);
        }
    }
    

提交回复
热议问题