简体   繁体   English

React,ESLint:eslint-plugin-react-hooks 依赖于 object 中的 function

[英]React, ESLint: eslint-plugin-react-hooks dependency on a function in an object

I'm not sure this is a bug, but I need an explanation then.我不确定这是一个错误,但我需要一个解释。 Consider the following code:考虑以下代码:

const someCallback = useCallback(() => console.log(someObj.someChildObject), [someObj.someChildObject])

ESLint rule does not give any warning about missing or wrong dependencies, however, the following code gives a warning about missing someObj dependency: ESLint 规则不会给出任何有关缺少或错误依赖项的警告,但是,以下代码会给出有关缺少someObj依赖项的警告:

const someCallback = useCallback(() => someObj.someChildFunction(), [someObj.someChildFunction])

Can someone explain, why does the second example produce a warning?有人可以解释一下,为什么第二个例子会产生警告? Or is it actually a bug?或者它实际上是一个错误? Using 4.0.8 version of eslint-plugin-react-hooks package使用 4.0.8 版本的eslint-plugin-react-hooks package

Can someone explain, why does the second example produce a warning?有人可以解释一下,为什么第二个例子会产生警告?

Though the function might be the same the result it produces might depend on the calling context ie someObj .尽管 function 可能相同,但它产生的结果可能取决于调用上下文,即someObj

Consider the following example for simplicity.为简单起见,请考虑以下示例。

// assume someObj is just a number and someChildFunction is toString
someObj = Math.random();

// you can't memoize based on the function itself because it uses context
const someCallback = useCallback(() => someObj.toString(), [someObj.toString])

toString is the same for every number 5..toString === 4..toString but the result it produces depends on the number. toString对于每个数字5..toString === 4..toString都是相同的,但它产生的结果取决于数字。

暂无
暂无

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

相关问题 Eslint React Hooks错误:eslint-plugin-react-hooks用尽详尽的警告警告useEffect中的功能依赖项 - Eslint React Hooks Error: eslint-plugin-react-hooks exhaustive deps warning for a function dependency in useEffect 带有依赖列表和 eslint-plugin-react-hooks 的自定义钩子 - Custom hooks with dependency lists and eslint-plugin-react-hooks React,ESLint:eslint-plugin-react-hooks 显示不正确的“缺少依赖项” - React, ESLint: eslint-plugin-react-hooks shows incorrect “missing dependency” 为什么 eslint-plugin-react-hooks 在有条件地使用 React 钩子时不会发出警告? - Why eslint-plugin-react-hooks doesn't warn when using react hooks conditionally? eslint-plugin-react-hooks 出现意外错误(react-hooks/exhaustive-deps) - eslint-plugin-react-hooks is giving unexpected errors (react-hooks/exhaustive-deps) 如何让 `eslint-plugin-react-hooks` 对导出为 `default` 的功能组件进行 lint? - How can I get `eslint-plugin-react-hooks` to lint functional components that are exported as `default`? eslint-plugin-react-hooks 是否在启动时显示代码本身或终端上的错误? - Does eslint-plugin-react-hooks displays the errors on the code itself or just the terminal when starting? 与useEffect一起使用时如何防止触发useCallback(并遵守eslint-plugin-react-hooks)? - How to prevent useCallback from triggering when using with useEffect (and comply with eslint-plugin-react-hooks)? eslint-plugin-react是否需要eslint的同行? - eslint-plugin-react requires a peer of eslint? 在打字稿中启用react-hooks eslint插件。 tslint中没有插件 - enabling the react-hooks eslint plugin in typescript. no plugins in tslint
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM