[英]react-hooks/exhaustive-deps warning with custom IntersectionObserver hook
I have this custom react hook copied more of less straight from this dev.to article for use of the IntersecionObserver
in React.我从这篇 dev.to 文章中直接复制了这个自定义反应钩子,以便在 React 中使用
IntersecionObserver
。 article here 文章在这里
The Hook钩子
import { useEffect, useRef, useState } from "react"
export const useIntersectionObserver = (options) => {
const containerRef = useRef(null)
const [isVisible, setIsVisible] = useState(false)
const callback = (entries) => {
const [entry] = entries
setIsVisible(entry.isIntersecting)
}
useEffect(() => {
let currentEl;
const observer = new IntersectionObserver(callback, options);
if (containerRef && containerRef.current) {
currentEl = containerRef.current;
observer.observe(currentEl);
}
return () => {
if (containerRef && containerRef.current) {
currentEl = containerRef.current;
observer.unobserve(currentEl);
}
}
}, [containerRef, options])
return [containerRef, isVisible]
}
in my console I have this warning,在我的控制台中,我有这个警告,
The ref value 'containerRef.current' will likely have changed by the time this effect cleanup function runs.
到此效果清理 function 运行时,参考值“containerRef.current”可能已更改。 If this ref points to a node rendered by React, copy 'containerRef.current' to a variable inside the effect, and use that variable in the cleanup function react-hooks/exhaustive-deps
如果此 ref 指向 React 渲染的节点,请将“containerRef.current”复制到效果内的变量中,并在清理 function react-hooks/exhaustive-deps 中使用该变量
Can someone please provide a more in-depth explanation as to what exactly this means or point me to a resource?有人可以就这到底意味着什么提供更深入的解释或将我指向资源吗? As well as the solution to satisfy the error requirement.
以及满足错误要求的解决方案。 From what I can see
containerRef.current
IS referenced inside the useEffect
cleanup, clearly I do not have a solid understanding of what is actually happening here.从我可以看到在
useEffect
清理中引用了containerRef.current
,显然我对这里实际发生的事情没有深入的了解。
The warning exists because, in some cases, the .current
value referenced in the body of an effect will be different from the .current
value referenced in the cleanup function.存在警告是因为在某些情况下,效果主体中引用的
.current
值将不同于清理 function 中引用的.current
值。 In general, if you do一般来说,如果你这样做
useEffect(() => {
// code that references someRef.current
return () => {
// more code that references someRef.current
};
you may well be counting on both such references to reference the same value - but that's not guaranteed, and will sometimes be a source of bugs.您可能会指望这两个引用来引用相同的值 - 但这并不能保证,有时会成为错误的来源。
In your code, there is only one line in the code which throws the error, which is this one:在您的代码中,代码中只有一行引发错误,即这一行:
return () => {
if (containerRef && containerRef.current) {
currentEl = containerRef.current; // <--------
observer.unobserve(currentEl);
}
}
Which is reasonable - there's no absolute guarantee that your这是合理的 - 不能绝对保证您的
currentEl = containerRef.current;
observer.observe(currentEl);
in the effect body will be the same element unobserved in the cleanup function.在效果体中将是清理 function 中未观察到的相同元素。
The fix would be to extract the .current
value into a variable in the effect body, and reference that variable in the cleanup.解决方法是将
.current
值提取到效果主体中的变量中,并在清理中引用该变量。
Also note that a ref will always be truthy - no need to test it.另请注意,参考将始终是真实的 - 无需对其进行测试。
useEffect(() => {
const currentEl = containerRef.current;
if (!currentEl) return; // no need to attach observer or cleanup
const observer = new IntersectionObserver(callback, options);
observer.observe(currentEl);
return () => {
observer.unobserve(currentEl);
};
}, [containerRef, options])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.