gpt4 book ai didi

javascript - react : Unmount event listener from server

转载 作者:行者123 更新时间:2023-12-03 07:58:09 25 4
gpt4 key购买 nike

在我的 app.js 中,我正在安装事件监听器。几次重新加载后,我收到“警告:检测到可能的 EventEmitter 内存泄漏。添加了 11 个更改监听器”。

我的代码是这样的。

  let App = React.createClass({

componentWillMount() {
ArticleStore.addChangeListener((e) => this.onArticleChange(e));
},

componentWillUnmount() {
ArticleStore.removeChangeListener(e);
}

}

但是,componentWillUnmount 永远不会在 app.js 中被调用,因为它存在于服务器上,并且我最终打开了很多事件监听器,导致上述错误并超出了我的事件监听器限制。

如何卸载 app.js 中的任何事件监听器?

最佳答案

componentWillMount 在服务器上被调用,但 componentDidMount 未被调用。为了防止泄漏,您通常需要使用 componentDidMount 进行事件注册。

let App = React.createClass({

componentDidMount() {
ArticleStore.addChangeListener((e) => this.onArticleChange(e));
},

componentWillUnmount() {
ArticleStore.removeChangeListener(e);
}

}

关于javascript - react : Unmount event listener from server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34713966/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com