当前位置:网站首页>The ref value ‘xxx‘ will likely have changed by the time this effect function runs.If this ref......
The ref value ‘xxx‘ will likely have changed by the time this effect function runs.If this ref......
2022-07-28 19:33:00 【霜雪遥】
一. 报错场景
在react中,使用useEffect监听、清除鼠标事件时出现的警告。代码如下:
import {
useEffect, useRef } from 'react'
const homePageRef = useRef<HTMLDivElement | null>(null)
useEffect(() => {
if (navigator.userAgent.indexOf('Firefox') === -1) {
homePageRef.current?.addEventListener(
'mousewheel',
_.debounce(scrollChange, 100, {
trailing: false, leading: true }),
true
)
} else {
homePageRef.current?.addEventListener(
'DOMMouseScroll',
_.debounce(scrollChange, 100, {
trailing: false, leading: true }),
true
)
}
return () => {
if (navigator.userAgent.indexOf('Firefox') === -1) {
homePageRef.current?.removeEventListener('mousewheel', scrollChange, true)
} else {
homePageRef.current?.removeEventListener('DOMMouseScroll', scrollChange, true)
}
}
}, []) // eslint-disable-line react-hooks/exhaustive-deps
报如下警告:

意思是useEffect在执行清除功能时,homePageRef.current可能发生了改变。如果这个 ref 指向由 React 渲染的节点,将 ‘homePageRef.current’ 在useEffect中赋值给一个变量,在清除函数中使用该变量。
二. 解决如下
修改为如下代码后不再报出警告
import {
useEffect, useRef } from 'react'
const homePageRef = useRef<HTMLDivElement | null>(null)
useEffect(() => {
const homePageHTML = homePageRef.current
if (navigator.userAgent.indexOf('Firefox') === -1) {
homePageHTML?.addEventListener(
'mousewheel',
_.debounce(scrollChange, 100, {
trailing: false, leading: true }),
true
)
} else {
homePageHTML?.addEventListener(
'DOMMouseScroll',
_.debounce(scrollChange, 100, {
trailing: false, leading: true }),
true
)
}
return () => {
if (navigator.userAgent.indexOf('Firefox') === -1) {
homePageHTML?.removeEventListener('mousewheel', scrollChange, true)
} else {
homePageHTML?.removeEventListener('DOMMouseScroll', scrollChange, true)
}
}
}, []) // eslint-disable-line react-hooks/exhaustive-deps
(完)
边栏推荐
- Study and use of cobalt strike
- C#连接MySql数据库详细步骤
- How NPM switches Taobao source images
- C # basic 6-file IO and JSON
- quii cordova-plugin-telerik-imagepicker插件多图上传乱序
- Uncaught Error:Invalid geoJson format Cannot read property ‘length‘ of undefind
- 如何度量软件架构
- Unity foundation 6-rotation
- A 58 year old native of Anhui Province, he has become the largest IPO investor in Switzerland this year
- 编码用这16个命名规则能让你少写一半以上的注释!
猜你喜欢

Unit editor details

Ctfshow network lost track record (2)

MoCo V2:MoCo系列再升级

什么是 CI/CD? | 实现更快更好的软件交付

九鑫智能正式加入openGauss社区

Young freshmen yearn for more open source | here comes the escape guide from open source to employment!

Cause analysis of restart of EMC cx4-120 SPB controller

Eureka相互注册,只显示对方或只在一个中显示问题

58岁安徽人,干出瑞士今年最大IPO 投资界

Interpretation of netappp SP sensors output content
随机推荐
What is low code? Which platforms are suitable for business personnel? Is it reliable to develop the system?
怎样搭建企业内部维基百科
There have been two safety accidents in a month after listing. Is L9 ideal?
向往的开源之多YOUNG新生 | 从开源到就业的避坑指南来啦!
A 58 year old native of Anhui Province, he has become the largest IPO investor in Switzerland this year
mysql还有哪些自带的函数呢?别到处找了,看这个就够了。
关键路径的分析
MoCo V1:视觉领域也能自监督啦
Unity foundation 6-rotation
Maxwell is an easy-to-use software for capturing MySQL data in real time
C # basic 6-file IO and JSON
(turn) bubble sorting and optimization details
Guanghetong & Qualcomm Internet of things technology open day successfully held
Redis缓存雪崩、缓存穿透、缓存击穿
Color finder actual combat (QT including source code)
Dom4J的Bug
使用缓冲的方式采集视频
微服务架构下的系统集成
It is not only convenient, safe + intelligent, but also beautiful. Fluorite releases the Big Dipper face lock dl30f and Aurora face video lock y3000fv
[tidb] importing TXT documents into the database is really efficient