简体   繁体   English

带有自定义 IntersectionObserver 钩子的 react-hooks/exhaustive-deps 警告

[英]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.

相关问题 反应 useEffect 带有警告的钩子 react-hooks/exhaustive-deps - React useEffect hook with warning react-hooks/exhaustive-deps 设计React Hooks可以防止React-Hooks / Exhaustive-deps警告 - Designing React Hooks prevent react-hooks/exhaustive-deps warning 如何解决`react-hooks/exhaustive-deps`的`React Hook useEffect缺少依赖`? - How to solve `React Hook useEffect has a missing dependency` of `react-hooks/exhaustive-deps`? useEffect 和 &#39;react-hooks/exhaustive-deps&#39; linting - useEffect and 'react-hooks/exhaustive-deps' linting react-hooks/exhaustive-deps 导致依赖警告,修复挂起代码 - react-hooks/exhaustive-deps causing dependency warning, fix hangs code 未找到规则“re​​act-hooks/exhaustive-deps”的定义 - Definition for rule 'react-hooks/exhaustive-deps' was not found React Hook useEffect 缺少依赖项:&#39;formValues&#39;。 包括它或删除依赖数组 react-hooks/exhaustive-deps - React Hook useEffect has a missing dependency: 'formValues'. Either include it or remove the dependency array react-hooks/exhaustive-deps React Hook useEffect 缺少依赖项:“roomID”和“sotreId”。 要么包含它们,要么删除依赖数组 react-hooks/exhaustive-deps - React Hook useEffect has missing dependencies: 'roomID 'and 'sotreId'. Either include them or remove the dependency array react-hooks/exhaustive-deps React Hooks react-hooks/exhaustive-deps eslint 规则似乎过于敏感 - React Hooks react-hooks/exhaustive-deps eslint rule seems overly sensitive 如何在React中使用钩子实现componentDidMount以使其符合EsLint规则“ react-hooks / exhaustive-deps”:“ warn”? - How to implement componentDidMount with hooks in React to be in line with the EsLint rule “react-hooks/exhaustive-deps”: “warn”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM