Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Anchor和Affix组件,当进行路由切换时,控制台警告存在内存泄漏? #38894

Closed
Jauuo opened this issue Nov 23, 2022 · 1 comment

Comments

@Jauuo
Copy link

Jauuo commented Nov 23, 2022

Reproduction link

Edit on CodeSandbox

Steps to reproduce

1.先点击Affix
2.然后点击Index
3.查看控制台
(1,2两步可能需多次尝试会出现)

What is expected?

希望没有警告

What is actually happening?

控制台:
Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method.

Environment Info
antd 4.20.1
React 17.0.2
System window
Browser Chrome (Latest)
@zombieJ
Copy link
Member

zombieJ commented Nov 27, 2022

No warning about setState on unmounted components: Previously, React warned about memory leaks when you call setState on an unmounted component. This warning was added for subscriptions, but people primarily run into it in scenarios where setting state is fine, and workarounds make the code worse. We've facebook/react#22114 this warning.

ref: facebook/react#24195

大部分的卸载后 setState 都不会造成内存泄漏,反而是解决这个 warning 会造成糟糕的代码。在 React 18 中这个 warning 已经被移除了。

同样的,Affix 里有个 ResizeObserver 来检测容器尺寸变化,在移除时触发了异步的尺寸计算,因而触发了旧版本 React 的 warning。但是实际上当元素卸载后,ResizeOberver 组件也会被卸载,并不会有内存泄漏。加上 mount ref 反而会有额外的性能检查损耗。

@zombieJ zombieJ closed this as completed Nov 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants