-
Notifications
You must be signed in to change notification settings - Fork 46.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ESLint] Treat functions that don't capture anything as static #14996
Conversation
Details of bundled changes.Comparing: f16442a...c30cf04 eslint-plugin-react-hooks
Generated by 🚫 dangerJS |
Perhaps, a better alternative could be to make the autofix wrap safe-but-used-in-effects/callbacks functions into The upside of that would be that if you add a prop to some deep function in the call stack, it propagates to immediate callers. So you don't have an avalanche of transitively calling effects to fix. But the downside is you don't see which effects depend on that prop transitively. However, that may be good. Because effects might get reported with coarser deps than necessary otherwise. Having Another risk is it could guide you further from It's hard to say without trying. |
// const ref = useRef() | ||
// ^^^ true for this reference | ||
// False for everything else. | ||
function isStaticKnownHookValue(resolved) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function just moved into the closure. It's the same implementation with a slightly different argument.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#nakazawayeah
The exact heuristics will take me some time to figure out. This is just the first minimal draft.
It lifts the restriction on specifying functions as deps where it's safe. In particular, a function like
would be considered safe because it doesn't capture any render-phase values except knowingly static ones (we only do this if
setCount
comes fromuseState
Hook).So we could omit it:
But if we change
tick
to beit would warn, asking you to add
tick
to dependencies.We only do this check one level deep. So it helps for helper functions that only set state or dispatch, but you might end up in a situation where you can't add a prop to one of them without triggering warnings for all effects using it. Ideally we might want to push you to
useReducer
a bit sooner but I haven't figured out what a good flow is.This PR makes the rule strictly more relaxed. It doesn't add any new behaviors, it just fires the warnings less often in cases where it's safe.
In follow-ups, we'll need to tweak the behavior and warnings to be more useful for functions. Such as we might want to detect the above special case and suggest the updater form if it would fix the warning. Similarly, we could detect always-new deps (like
tick
) and in that case suggest wrapping inuseCallback
.