当前位置:网站首页>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 )
边栏推荐
- SQL Server 数据库之备份和恢复数据库
- Maxwell 一款简单易上手的实时抓取Mysql数据的软件
- 多线程顺序运行的 4 种方法,面试随便问
- Maintenance of delta hot metal detector principle analysis of v5g-jc-r1 laser measurement sensor / detector
- Zcmu--5066: dark corridor
- System integration under microservice architecture
- 58岁安徽人,干出瑞士今年最大IPO 投资界
- 向往的开源之多YOUNG新生 | 从开源到就业的避坑指南来啦!
- Kubeadm搭建kubernetes集群
- Source insight uses shortcut keys
猜你喜欢

Backup and recovery of SQL Server database

BUUCTF做题Upload-Labs记录pass-01~pass-10
![[cloud native] what is ci/cd| Ci/cd to smooth delivery obstacles](/img/4f/e7806d75cd719e181d8455e4fdc1e7.jpg)
[cloud native] what is ci/cd| Ci/cd to smooth delivery obstacles

How to build a foreign environment for the self-supporting number of express evaluation? How much does it cost?

How to modify the ID of NetApp expansion enclosure disk shelf

protobuf 中基础数据类型的读写

ctfshow 网络迷踪做题记录(1)

Eureka registers with each other, only showing each other or only showing problems in one

(PMIC)全、半桥驱动器CSD95481RWJ PDF 规格

Confession of a graduate student: why am I addicted to opengauss community?
随机推荐
uniapp的进度条自定义
4.1 various calling methods of member
一名在读研究生的自白:我为什么会沉迷于openGauss 社区?
Four methods of multi-threaded sequential operation. Ask casually during the interview
Basic operations of unity3d scene production
Mobilevit: challenge the end-to-side overlord of mobilenet
Deit: attention can also be distilled
Another installation artifact
Maintenance of delta hot metal detector principle analysis of v5g-jc-r1 laser measurement sensor / detector
CVPR 2022 | in depth study of batch normalized estimation offset in network
Guanghetong & Qualcomm Internet of things technology open day successfully held
source insight 使用快捷键
实习日记第一周
30.学习Highcharts 标签旋转柱形图
SQL Server 数据库之备份和恢复数据库
Unity3d tutorial notes - unity initial 03
Top level "redis notes", cache avalanche + breakdown + penetration + cluster + distributed lock, Nb
【题目】两数相加
Study and use of cobalt strike
(turn) bubble sorting and optimization details