当前位置:网站首页>Componentwillunmount uninstall components
Componentwillunmount uninstall components
2022-04-23 00:12:00 【Buliang_ C】
problem :index.js:1 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.
( Warning : Cannot execute on an unloaded component React Status update . This is an empty operation , But it indicates a memory leak in your application . To solve this problem , Please be there. componentWillUnmount Method to cancel all subscriptions and asynchronous tasks .)

Problem description : This is after the component is uninstalled , Proceed again React Of state Status update for , This is obviously right React Is an empty operation for the component , There are no more components available to update the state and render , So the above error will be reported .
Similar to this problem is , stay React Before the stateful component of is loaded , Change the state of components in advance setState, This will make it impossible to determine whether there is a page rendering behind . Make data changes before the component is loaded , But now, componentWillMount() This lifecycle function has been removed .
To take one React The small example , Explain :
class Clock extends React.Component {
constructor() {
super()
this.state = {
now: 0
}
}
// Clear the timer before unloading the component , Otherwise, it will be updated after the component is uninstalled state, Cause memory leaks
componentWillUnmount() {
clearInterval(this.timeId)
}
componentDidMount() {
// var that = this
var func = function() {
var now = new Date().toLocaleTimeString()
this.setState({
now
})
}.bind(this)
func()
// Every interval 1s Do it once func() Method
this.timeId = setInterval(func, 1000)
}
render() {
return (
<div className="container">
{
this.state.now}
</div>
)
}
}
Display the current time in real time on the page ,componentDidMount() Timer in , Change every second state state , Component aware state changes , Then re render the components . At the current moment of component unloading , perform componentWillMount() The content in , Clear timer timeId. inform React The component is about to be uninstalled , Do not update the status after uninstallation .
React Official website componentWillUnmount
版权声明
本文为[Buliang_ C]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230008119917.html
边栏推荐
- 人为什么看不到事实?
- npm 和 npx 查看依赖包版本
- (转)Word2016怎么和mathtype兼容
- WMS仓储管理系统解决方案
- ARP地址解析协议
- 编程2022-02 KTV
- 条码WMS系统与ERP接口实现方法
- 【ACM】47. Full Permutation II (consider whether the recorded elements under a branch will be repeatedly recorded when recursing at the next level (use the array used to record))
- 对指定目录下的图片,取图片名(不包含图片的扩展名)、将文件路径和图片名分开、将文件名和图片扩展名分开
- 90% of test engineers use postman for interface testing
猜你喜欢

Antd design pro uses Baidu map, which can be tested in person

Antd Design pro使用百度地图,亲测可用

A ConvNet for the 2020s的总结

【ACM】90. Subset II (the de duplication problem of the same tree layer first needs to sort the array (use sort))

100000 developers swarmed into "cool applications" to bet on scenarios

MySQL运行workbench报错TRunTimeError

WMS仓储管理系统解决方案

绝对定位不使用left,right,top,bottom等属性

Swin Transformer网络架构、相应改进模块的理解

三菱MR-JE-C伺服应用详细介绍
随机推荐
Implementation method of interface between bar code WMS system and ERP
力扣习题集1--两数之和
What kind of community play do you do?
(转)Win10上安装任意版本的.net framework
【leetcode】27. Removing Elements
倍福EL5101模块获取光栅尺反馈位置并绑定到NC轴配置
C#控制台应用程序运行时一闪而过解决方法
Vibrato tiktok
MySQL运行workbench报错TRunTimeError
B端/C端中,产品or运营哪个更重要?
【ACM】78. 子集(返回所有节点的元素,不用设置终止条件)
教你两分钟做出一个精美好用的404页面
Change key for complex array structure data
Utiliser dottrace 6. 0 performance, analyse de mémoire
(转)通过RDP隧道绕过网络限制
Shell脚本笔记(5)- 本的条件语句
【ACM】90. Subset II (the de duplication problem of the same tree layer first needs to sort the array (use sort))
两种方法实现mongodb自增id,推荐第二种
(transfer) Aspose Documentbuilder I of words Programming Guide
OJ's daily practice -- falling and bouncing ball