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 to destructure props when they are only used as members #14993

Merged
merged 3 commits into from Mar 1, 2019

Conversation

gaearon
Copy link
Collaborator

@gaearon gaearon commented Mar 1, 2019

This addresses some confusion we've seen around props.foo() asking for props to be included. It's also best practice anyway. But only show the warning if props isn't used as an object. (And only if it's actually missing — so that it doesn't complain for valid code.)

@sizebot
Copy link

sizebot commented Mar 1, 2019

Details of bundled changes.

Comparing: 59ef284...9e795c8

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.1% +1.9% 56.57 KB 57.77 KB 13.19 KB 13.45 KB NODE_DEV
eslint-plugin-react-hooks.production.min.js 🔺+2.5% 🔺+2.1% 14.21 KB 14.57 KB 4.95 KB 5.05 KB NODE_PROD
ESLintPluginReactHooks-dev.js +2.1% +1.8% 60.22 KB 61.48 KB 13.53 KB 13.78 KB FB_WWW_DEV

Generated by 🚫 dangerJS

Copy link
Contributor

@threepointone threepointone left a comment

Choose a reason for hiding this comment

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

nice. does this have to be for 'props' only? I"m sure that mooost people use 'props', but it could be named anything really.
looks good tho! making it a point to dive into the code a bit deeper next week.

@gaearon
Copy link
Collaborator Author

gaearon commented Mar 1, 2019

I don't think it's worth warning if it's called something other than props (personally have never seen that). Conversely, I think it's fine to warn if a variable just happens to be called props.

@gaearon gaearon merged commit e1e45fb into facebook:master Mar 1, 2019
@@ -2224,7 +2224,189 @@ const tests = {
}
`,
errors: [
// TODO: make this message clearer since it's not obvious why.
"React Hook useEffect has a missing dependency: 'props'. " +
Copy link
Contributor

Choose a reason for hiding this comment

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

This error message kinda surprises me when taking into account our recent discussion ( #14876 ) around props referential equality etc.

If props have "unstable" reference then IMHO you shouldnt ever suggest using it as hook's dependency 🤔 The latter advice sounds way more appropriate (destructuring)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

props is OK as an escape hatch when you just want the rule to shut up. It's still stable if you only set state, for example. Better than nothing.

Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldnt the advice get reordered then? First propose destructuring and only latter propose using props directly.

You are calling it an escape hatch to shut up the rule - so it sounds to me that this is actually not something which you'd like to recommend.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

You're welcome to send a PR. :-) There are more worrying things I'm currently focused on than this particular message.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sure, might prepare one - was just validating my thinking so far :)

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

5 participants