This rule is not always applicable - functions cannot be hoisted if they depend on (capture) any other variable. If they are not wrapped in useCallback, they will not be stable.
In this example, handleSnapshot must be wrapped in useCallback to be stable, but it cannot be hoisted out of the function because it uses setShowSnapshot. I could add setShowSnapshot to the dependencies list, but it's a known-stable reference (from useState) and thus doesn't need to be provided.
This rule is not always applicable - functions cannot be hoisted if they depend on (capture) any other variable. If they are not wrapped in
useCallback
, they will not be stable.Example
In this example,
handleSnapshot
must be wrapped inuseCallback
to be stable, but it cannot be hoisted out of the function because it usessetShowSnapshot
. I could addsetShowSnapshot
to the dependencies list, but it's a known-stable reference (fromuseState
) and thus doesn't need to be provided.