项目场景:

使用React的NavLink标签作为菜单列表,点击菜单列表按钮,动态更换菜单名称


问题描述

点击菜单NavLink时报错出现如下信息

Warning:
This synthetic event is reused for performance reasons.
If you're seeing this, you're accessing the property `nativeEvent` on a released/nullified synthetic event.
This is set to null.
If you must keep the original synthetic event around, use event.persist().
See https://fb.me/react-event-pooling for more information.

例子

// 其他无关紧要部分省略了
<input onChange={event => {console.log(event.type)  // changesetTimeout(() => {console.log(event.type)  // null,并会出现以上警告})
}} />

原因分析:

我们在react中操作的DOM事件,获取到的事件对象,其实是react内部帮我们合成的。为了节约性能,会使用对象池,当一个合成事件对象被使用完毕,即同步代码实现完毕后,会再次调用并且将其属性全部设为Null,所以当我们异步访问或者打印时,显示的属性值已经是null值


解决方案:

如果要以异步方式访问事件属性,应该对事件调用 event.persist(),这将从池中删除合成事件,并允许用户代码保留对事件的引用。它会将当前的合成事件对象从对象池抽离出来,这样就不会再被调用并且其属性不会变为Null.

将 event事件对象传入方法,

<div onClick={(event) => this.mouseOut('关注QC班长',event)}><NavLink to="/largeScreen/fireworks/home" activeClassName={styles.activeNav}><SlackOutlined style={{ marginRight: '5px' }} />关注QC班长</NavLink>
</div>

在方法中调用event.persist()即可

 mouseOut = (menuName,event) => {this.setState({ visible: 'none' })this.setState({ isActive: true })if (menuName !== '') {event.persist()// 从事件池中移除该合成函数并允许对该合成事件的引用被保留下来。this.setState({ activeMenuName: menuName })}}

参考文献

0、合成事件(SyntheticEvent) – React 中文文档 v16.6.3

1、react 合成事件关于event.persist()的报错_coderlin_的博客-CSDN博客

2、https://www.jianshu.com/p/bf390141fae8

3、2020/09/20 React中获取target(报错信息Warning: This synthetic event is reused for performance reasons.)_六月要好好加油呀的博客-CSDN博客

React报错Warning: This synthetic event is reused for performance reasons. If you‘re seeing this, 解决方法相关推荐

  1. 2020/09/20 React中获取target(报错信息Warning: This synthetic event is reused for performance reasons.)

    如下图,我要获取到点击的li 我的代码 return中的内容 函数 -这里要加上 event.persist() 否则只有第一次点击能够获取到target,再次会报错,显示target:null,报错 ...

  2. [react] This synthetic event is reused for performance reasons. If you‘re seeing this, you‘re access

    error This synthetic event is reused for performance reasons. If you're seeing this, you're accessin ...

  3. 【react】This synthetic event is reused for performance reasons.

    html代码 <form onSubmit={this.submit.bind(this)}><input type="text" name="user ...

  4. 报错:/BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3512.29.5/UITableView.m:7943解决方法

    报错:/BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3512.29.5/UITableView.m:7943解决方法 ...

  5. 【svn】svn报错:“Previous operation has not finished; run ‘cleanup‘ if it was interrupted“ 的解决方法

    [svn]svn报错:"Previous operation has not finished; run 'cleanup' if it was interrupted" 的解决方 ...

  6. Vue报错:Uncaught TypeError: Cannot assign to read only property’exports‘ of object’#Object‘的解决方法...

    发现问题 运行一下以前的一个Vue+webpack的 vue仿新闻网站  小项目,报错 由于自己vue学习不深入,老是这个报错,找了好久(确切的说是整整一下午^...^)才找到原因 -v- Uncau ...

  7. mysql报错:This version of MySQL doesn’t yet support ‘LIMIT IN/ALL/ANY/SOME 解决方法

    最近使用MySQL5.6查询表的前几条数据,使用了limit语句,结果出现错误:This version of MySQL doesn't yet support 'LIMIT & IN/AL ...

  8. mysql报错ERROR 1206 (HY000): The total number of locks exceeds the lock table size的解决方法...

    1. 问题背景         InnoDB是新版MySQL(v5.5及以后)默认的存储引擎,之前版本的默认引擎为MyISAM,因此,低于5.5版本的mysql配置文件.my.cnf中,关于InnoD ...

  9. 登录mysql报错Failed to connect to backoff 或 Failed to get D-Bus connection: Operation not permitted解决方法

    报错: Failed to get D-Bus connection: Operation not permitted 或 mysql -u root -p 登录mysql时报错 2020-09-16 ...

最新文章

  1. 在3ds Max中使用V-Ray 5渲染引擎视频教程
  2. python数组对应元素相乘_python的几种矩阵相乘的公式详解
  3. win10键盘全部没反应_Win10笔记本键盘失灵怎么办 Win10键盘失灵解决方法【详解】...
  4. 解决Spring的java项目打包后执行出现“无法读取方案文档...“、“原因为 1) 无法找到文档; 2) 无法读取文档; 3) 文档的根元素不是...”问题
  5. Iterator迭代器原理
  6. python 识别数字字母 训练集_Python 实现训练集、测试集随机划分
  7. python 基础之字符串方法
  8. android popupwindow 调用方法,Android PopUpWindow使用详解
  9. 牛掰本机限速软件appband
  10. Spring AOP中的前置通知和后置通知详解
  11. [Python] L1-021. 重要的话说三遍-PAT团体程序设计天梯赛GPLT
  12. eclipse Maven搭建SSH框架
  13. xml格式校验工具_logback 日志输出格式
  14. elementui自定义手机号邮箱验证
  15. [Mac] Newifi mini路由器刷breed+Padavan固件
  16. ios label内字体置顶_IOS_Vertically align UILabel文本置顶 | 学步园
  17. unit怎么发音_unitl是什么意思
  18. Python学习记录-实战一、车牌归属地
  19. 如何用Python网络爬虫爬取网易云音乐歌词
  20. vs2013+opencv3.0.0 计算机丢失 opencv_world300d.dll

热门文章

  1. PHP对接国际验证码接口DEMO示例
  2. Sentinel持久化
  3. nginx域名重定向 实现新旧域名过渡
  4. 访问者模式Visitor
  5. Java--文件(File)
  6. oracle 日期函数
  7. 关于“/usr/include/openssl/bn.h 288 error: parse error before BN_ULONG解决方法
  8. JavaScript 实现web随机数排序、整除、重置
  9. 数据项组成数据元素,数据元素组成数据
  10. 音频文件 数据库存储_Apache Kafka是数据库吗?