简体   繁体   English

具有内存泄漏的React组件

[英]React Component with Memory Leak

I have a piece of legacy code, which renders a react component on the server on every request, which makes it obvious there is a memory leak. 我有一段遗留代码,它在每次请求时在服务器上呈现一个react组件,这显然存在内存泄漏。 I have corner the problem up to this code: 我已经解决了这个代码的问题:

  componentWillMount: function () {
    var onLogin = this.props.onLogin || function () {},
        onLogout = this.props.onLogout || function () {};

    this.on('authChange', function () {
      console.log('user authenticated:', this.state.isAuthenticated);
      return this.state.isAuthenticated
              ? onLogin(this.state)
              : onLogout(this.state);
    }.bind(this));
  },

I believe that on every request the this object is storing a new listener, but I don't get why the this element is not being marked as garbage when the rendering of the component is done. 我相信在每个请求中, this对象都存储了一个新的监听器,但我不明白为什么在完成组件的渲染时, this元素没有被标记为垃圾。

You need to unbind the authChange handler before the component is unmounted. 在卸载组件之前,需要取消绑定authChange处理程序。 You can do this in componentWillUnmount . 您可以在componentWillUnmount执行此操作。

Since you're creating the handler function using the first props that are passed in, you should save it to a property so you can unbind it later: 由于您使用传入的第一个道具创建处理函数,因此应将其保存到属性中,以便以后可以取消绑定:

  componentWillMount: function () {
    var onLogin = this.props.onLogin || function () {},
        onLogout = this.props.onLogout || function () {};

    this.authChange = function () {
      console.log('user authenticated:', this.state.isAuthenticated);
      return this.state.isAuthenticated
              ? onLogin(this.state)
              : onLogout(this.state);
    }.bind(this);

    this.on('authChange', this.authChange);
  },

  componentWillUnmount: function () {
      this.off('authChange', this.authChange);
      this.authChange = null;
  }

Note that when I saw this.on I thought you might be using jQuery but it's not clear how that would be the case. 需要注意的是,当我看到this.on我想你可能会使用jQuery,但目前尚不清楚这将是这种情况。 My answer uses this.off to detach the event listener but you should use whatever the corresponding method is in your framework. 我的回答使用this.off来分离事件监听器,但是您应该使用框架中相应方法的任何内容。

I would move your function into componentDidMount and add cleanup on componentWillUnmount 我会将您的函数移动到componentDidMount并在componentWillUnmount上添加清理

Important : componentWillMount is called on the server and client , but componentDidMount is only called on the client . 重要提示componentWillMount被称为服务器 客户端 ,但componentDidMount只调用客户端上。

If you're using eventListeners , setInterval or other functions that needs to be cleaned, put them in componentDidMount . 如果您正在使用eventListenerssetInterval或其他需要清理的函数,请将它们放在componentDidMount The server will not call componentWillUnmount and is usually the cause of memory leaks. 服务器不会调用componentWillUnmount ,通常是内存泄漏的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM