React,ESLint:eslint-plugin-react-hooks 对对象中函数的依赖

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

我不确定这是不是一个错误,但我需要一个解释。考虑以下代码:

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

ESLint 规则不会给出任何有关缺少或错误依赖项的警告,但是,以下代码会给出有关缺少 someObj 依赖项的警告:

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

谁能解释一下,为什么第二个例子会产生警告?或者它实际上是一个错误?使用 4.0.8 版本 eslint-plugin-react-hooks

Can someone explain, why does the second example produce a warning?

尽管函数可能相同,但它产生的结果可能取决于调用上下文,即 someObj

为简单起见,请考虑以下示例。

// 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对于每个数字都是一样的5..toString === 4..toString但是它产生的结果取决于数字。