React16.7 hooks初探之定时器引发的问题

Hello, thanks for your awesome work on mobx-react! ... an action asynchronously from a componentWillMount hook, the react component will not rerende,本文由@IT·平头哥联盟-首席填坑官∙苏南 分享,公众号:honeyBadger8,webpack4,从0配置到项目搭建,React中的setTimeout、setInterval的注意事项 , react组件 使用setTimeout 定时隐藏 (有demo) 初始化时 展示 1次 图片,1秒后,隐藏图片点击 按钮 展示 1次 图片,1秒后

前言

周末尝试了一下React新的hooks功能,来封装一个组件,遇到一个bug,所以记录一下过程!

报错如下:

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 a useEffect cleanup function.in Notification

大概意思是组件已经卸载了,但在卸载之后还执行了一个对组件更新的操作,这是一个无效的操作,但它表示应用程序中存在内存泄漏。要修复,请取消useEffect cleanup function.in Notification 中的所有订阅和异步任务
Can't perform a React state update on an unmounted component.Read the Motivation to learn why we’re introducing Hooks to React, react组件 使用setTimeout 定时隐藏 (有demo) 初始化时 展示 1次 图片,1秒后,隐藏图片点击 按钮 展示 1次 图片,1秒后

组件核心代码如下:


function Notification(props){
   
  var timer = null;
  const [visible, setVisible] = useState(false);
  let {
   title,description,duration,theme,onClose,}= props;
  let leave = (source='') => {
   
    clearTimeout(timer);
    setVisible(false);
    console.log("注意这里是 leave方法里,timer的id:"+timer,"事件的来源:",source);
    console.log("leave result:",timer);
    onClose&&onClose();//首席填坑官∙苏南的专栏 交流:912594095、公众号:honeyBadger8
  }
  
  let enter = (
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值