Skip to content
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] Suggest moving inside a Hook or useCallback when bare function is a dependency #15026

Merged
merged 2 commits into from
Mar 6, 2019

Conversation

gaearon
Copy link
Collaborator

@gaearon gaearon commented Mar 5, 2019

This adds a new warning when a bare function is added as a dependency. It's always useless to do it because you might as well omit the deps array completely.

function handleChange(value) { // Needed as a dep, but it's a bare function so always invalidates
  setState(value);
}

useEffect(() => {
  return Store.subscribe(handleChange)
}, [handleChange]);

If the function is only used inside the Hook, it nudges you to move it inside it. That's usually an intuitive fix because then you can start fixing its deps.

If the function is used outside the Hook too, it suggests wrapping in useCallback. That lets you do "whack-a-mole" fixing up a chain of callbacks until you see what's causing things to change. There is an autofix for a simple case (variable declaration) where we know we wouldn't mess up hoisting.

This may be a good way to learn the dependencies mental model. However, there may be situations where just following suggestions gets you in a suggestion loop (e.g. circular function deps). I'm not sure I understand the limitations of this model well. However, even if you can't just follow autofixes, it would highlight the problem you'd have to discover on your own anyway. So at least this helps you see it. We may add more nudges (e.g. to use reducer) on top of this later to help you find the right fix.

@sizebot
Copy link

sizebot commented Mar 6, 2019

Details of bundled changes.

Comparing: 5d49daf...8ff6aaf

eslint-plugin-react-hooks

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
eslint-plugin-react-hooks.development.js +6.9% +6.9% 61.18 KB 65.39 KB 14.16 KB 15.13 KB NODE_DEV
eslint-plugin-react-hooks.production.min.js 🔺+10.3% 🔺+9.6% 15.27 KB 16.83 KB 5.31 KB 5.82 KB NODE_PROD
ESLintPluginReactHooks-dev.js +7.2% +6.8% 65.21 KB 69.9 KB 14.54 KB 15.52 KB FB_WWW_DEV

Generated by 🚫 dangerJS

@gaearon gaearon changed the title [ESLint] Suggest useCallback when bare function is a dependency [ESLint] Suggest moving inside a Hook or useCallback when bare function is a dependency Mar 6, 2019
@gaearon gaearon merged commit 9b7e1d1 into facebook:master Mar 6, 2019
@gaearon
Copy link
Collaborator Author

gaearon commented Mar 6, 2019

I think this is a good compromise and helps learn the rules.

@gaearon gaearon deleted the usecallback branch March 6, 2019 23:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants