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