当前位置:网站首页>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 21:22:00 【Frost and snow】
One . Error reporting scenario
stay react in , Use useEffect monitor 、 Warning when clearing mouse events . The code is as follows :
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
Give the following warning :

intend useEffect When performing the clear function ,homePageRef.current It may have changed . If this ref Point to by React Rendered nodes , take ‘homePageRef.current’ stay useEffect Assign a value to a variable in , Use this variable in the cleanup function .
Two . The solution is as follows
No warning will be given after modifying to the following code
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
( End )
边栏推荐
- Several skills of API interface optimization
- The ref value ‘xxx‘ will likely have changed by the time this effect function runs.If this ref......
- npm如何切换淘宝源镜像
- 4.1 various calling methods of member
- Ctfshow network lost track record (1)
- Link with bracket sequence I (state based multidimensional DP)
- 小程序容器技术,让移动研发效率提升500%
- System integration under microservice architecture
- Bus, protocol, specification, interface, data acquisition and control system in industrial communication field
- Maxwell is an easy-to-use software for capturing MySQL data in real time
猜你喜欢

Why on earth is it not recommended to use select *?

又一款装机神器

Cloud security core technology

【云原生】什么是 CI/CD ? | 摆平交付障碍的 CI/CD

SSM use @async and create threadpooltaskexecutor thread pool

Ctfshow network lost track record (2)

Unity - script lifecycle

Buuctf questions upload labs record pass-11~pass-20

Applet container technology improves mobile R & D efficiency by 500%

ctfshow 做题 web模块 web11~web14
随机推荐
Applet container technology improves mobile R & D efficiency by 500%
Why on earth is it not recommended to use select *?
CVPR 2022 | 网络中批处理归一化估计偏移的深入研究
Quii Cordova plugin telerik imagepicker plug-in multi image upload out of sequence
取色器实战(Qt含源码)
35 道 MySQL 面试必问题图解,这样也太好理解了吧
编码用这16个命名规则能让你少写一半以上的注释!
The development of smart home industry pays close attention to edge computing and applet container technology
又一款装机神器
uniapp的进度条自定义
Jiuxin intelligence officially joined opengauss community
Niuke turns on the camera and the picture disappears a few seconds later | the picture flashes when the camera is turned on
npm如何切换淘宝源镜像
Unity3d tutorial notes - unity initial 03
Efficientformer: lightweight vit backbone
Backup and recovery of SQL Server database
Redis cache avalanche, cache penetration, cache breakdown
云安全核心技术
1162. 地图分析-非递归法
DELTA热金属检测器维修V5G-JC-R1激光测量传感器/检测仪原理分析