更新时间:2023-12-01 16:04:58
那里您的代码中有轻微的混淆
there is slight confusion in your code
componentDidMount(){
window.addEventListener("resize", this.updateDimensions.bind(this));
// first instance listening to event
}
componentWillUnmount(){
console.log("unmounting....");
window.removeEventListener('resize', this.updateDimensions.bind(this));
// second instance removed from listener here first!== second
}
试试这个
constructor(props) {
super(props);
this.updateDimensions = this.updateDimensions.bind(this);
}
componentDidMount(){
window.addEventListener("resize", this.updateDimensions);
// first instance listening to event
}
componentWillUnmount(){
console.log("unmounting....");
window.removeEventListener('resize', this.updateDimensions);
// same instance removed from listener here first == second
}