Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
374 views
in Technique[技术] by (71.8m points)

componentWillUnmount setTimeout 输出this.state , 为什么数据还在?

  componentWillUnmount() {
    setTimeout(() => {
       // 为什么还能看到 state 呢?
      console.log('componentWillUnmount', this.state);
    }, 5000);
  }

组件不是卸载了吗,为什么数据还在呢? 这是不是内存泄露了呢?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

当然不算是内存泄漏了,正是因为你这里还有一个引用,所以才没有被回收。

你这里执行完了之后,就没有了引用,然后就会被回收。

正好,你其实写了一个内存泄漏的例子console 其实就是一个内存泄漏的典型例子,虽然你代码没有引用了,但是控制台还保存着。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...