简体   繁体   English

在 useEffect 依赖项中缺少 ref 时没有 eslint 警告

[英]No eslint warning when ref is missing in useEffect dependency

I have one doubt about ref in useEffect .我对useEffect ref有一个疑问。 Do I need to add it to dependency?我需要将它添加到依赖项吗?

const App = () => {
  const ref = useRef();
  useEffect(() => {
    //do something about ref
  }, []); //<-- ref is not here
}

the above code doesn't give any eslint warning.上面的代码没有给出任何 eslint 警告。 is ref exempted in the dependency? ref在依赖项中是否被豁免?

The short answer is yes, you don't have to add ref to the dependencies array.简短的回答是肯定的,您不必将ref添加到依赖项数组。

For deeper understanding you can read A Complete Guide to useEffect by Dan Abramov.为了更深入地理解,您可以阅读 Dan Abramov 的A Complete Guide to useEffect A quote from there, that answers your question:从那里引用,可以回答您的问题:

(You may omit dispatch, setState, and useRef container values from the deps because React guarantees them to be static. But it also doesn't hurt to specify them.) (你可以从 deps 中省略 dispatch、setState 和 useRef 容器值,因为 React 保证它们是静态的。但指定它们也没有坏处。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM