useEffect、useCallback、useMemo的依赖数组

The dependency array of useEffect, useCallback, useMemo

我对 React Hooks 的依赖性很困惑。 这是示例:

const memorizeValue = useMemo(() => {
    return {
      count,
      setCount,
    }
  }, [count, setCount])

在 React 纪录片中:

Note

The array of dependencies is not passed as arguments to the function. Conceptually, though, that’s what they represent: every value referenced inside the function should also appear in the dependencies array. In the future, a sufficiently advanced compiler could create this array automatically.

countsetCount 都在 useMemo 的回调中,如果我没有在依赖数组中传递 setCount,eslint 不会警告我,但是 count 会,所以有什么区别? 为什么不需要传递 setCountsetCount 在 useMemo 里面,不是吗?

这是代码框 link:

https://codesandbox.io/s/react-codesandbox-forked-xfupk?file=/src/Demo1/index.js:254-363

我想我可能误解了文档。有人指出,谢谢。

勾选useState docs

Note

React guarantees that setState function identity is stable and won’t change on re-renders. This is why it’s safe to omit from the useEffect or useCallback dependency list.

假设 setCountuseState 钩子的更新函数,那么它保证是一个稳定的引用。