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] Warn about setState directly in dep-less useEffect #15184

Merged
merged 1 commit into from Mar 22, 2019

Conversation

gaearon
Copy link
Collaborator

@gaearon gaearon commented Mar 21, 2019

This warns about the common mistake of using setState directly in an effect without a dependency list — which too easily leads to loops. This is the only case where I propose that we'd warn for dep-less useEffect. It looks like this:

Screen Recording 2019-03-21 at 08 53 PM

It won't warn you about setState nested in another function (it'll assume you know what you're doing). Only top level ones right in the effect.

It doesn't check for early returns. I considered this but then thought the pattern is still too fragile. With autofix it's not too annoying anyway.

@gaearon gaearon requested a review from bvaughn March 21, 2019 20:55
@gaearon gaearon changed the title Warn about setState directly in dep-less useEffect [ESLint] Warn about setState directly in dep-less useEffect Mar 21, 2019
@@ -468,9 +468,101 @@ export default {
},
);

// Warn about assigning to variables in the outer scope.
// Those are usually bugs.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code just moved from below with no changes.

if (!declaredDependenciesNode) {
// Check if there are any top-level setState() calls.
// Those tend to lead to infinite loops.
let setStateInsideEffectWithoutDeps = null;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is new.

@sizebot
Copy link

sizebot commented Mar 21, 2019

Details of bundled changes.

Comparing: 78968bb...78de1f3

eslint-plugin-react-hooks

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
eslint-plugin-react-hooks.development.js +2.8% +2.1% 73.68 KB 75.73 KB 16.93 KB 17.28 KB NODE_DEV
eslint-plugin-react-hooks.production.min.js 🔺+3.2% 🔺+3.0% 19.39 KB 20.01 KB 6.72 KB 6.93 KB NODE_PROD
ESLintPluginReactHooks-dev.js +2.8% +2.2% 78.94 KB 81.18 KB 17.42 KB 17.81 KB FB_WWW_DEV

Generated by 🚫 dangerJS

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.

None yet

4 participants