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

react-hooks-eslint-rule: add component props to pureScopes (#18051), … #19462

Closed

Conversation

delca85
Copy link
Contributor

@delca85 delca85 commented Jul 27, 2020

…update yarn.lock

Summary

Following #18051 comment by an unexpected behavior is found related code like this:

function Example({ sortBy }) {
  const foo = () => useMemo(() => console.log(sortBy), [sortBy])
 }

An error message is thrown since sortBy is evaluated as an unnecessary dependency while it is a necessary dependency.

According to me this was happening because pureScopes array did not contain the scope where sortBy is defined. I think this is a bug because component props (as well as argument received by a custom hook) should be contained by pureScopes array.
I changed the code in order to make these scopes always contained by pureScopes array.

Test Plan

I have tested the desired behavior is obtained and even that Component props are defined as necessary only if they are used by the hook.

{
code: normalizeIndent`
function Example({ sortBy }) {
const foo = () => useMemo(() => console.log(sortBy), [sortBy])
Copy link
Collaborator

Choose a reason for hiding this comment

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

The thing that's odd about these examples is that they break the Rules of Hooks. Concretely, here we have a Hook call inside a callback. But Hook calls should only happen at the top level.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it's totally right. So there is no bug but only a wrong hook usage.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Given that, I think I could close this PR because it eventually fixes a situation caused by a wrong Hook usage.
Since Hook calls should only happen at the top level, component props will be alway in pureScopes array.

Copy link
Contributor Author

@delca85 delca85 Jul 28, 2020

Choose a reason for hiding this comment

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

@gaearon sorry for coming back to this topic, I have just one more question.
Is the use I made in this sandbox breaking the hook rule? Because I don't receive any message from rules-of-hooks and I am thinking it could be a legit use (even in a really contrived example) because the hook is called twice a render.

Sorry again if I am missing something.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Totally misunderstood.
These examples are breaking the Rules of Hooks because the hook is called inside a function that is not a custom hook and neither in a JSX component.
The reason why the example in CodeSandbox is working is that the function within the hook is called begins with use. So...double error.

Sorry for the waste of time.

@codesandbox-ci
Copy link

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 65aaacf:

Sandbox Source
React Configuration

@sizebot
Copy link

sizebot commented Jul 27, 2020

Details of bundled changes.

Comparing: d29bf59...65aaacf

eslint-plugin-react-hooks

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
eslint-plugin-react-hooks.development.js +0.6% +0.6% 84.4 KB 84.87 KB 19.37 KB 19.48 KB NODE_DEV
eslint-plugin-react-hooks.production.min.js 🔺+0.6% 🔺+0.7% 23.33 KB 23.47 KB 7.9 KB 7.95 KB NODE_PROD

Size changes (experimental)

Generated by 🚫 dangerJS against 65aaacf

@sizebot
Copy link

sizebot commented Jul 27, 2020

Details of bundled changes.

Comparing: d29bf59...65aaacf

eslint-plugin-react-hooks

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
eslint-plugin-react-hooks.development.js +0.6% +0.6% 84.38 KB 84.86 KB 19.36 KB 19.47 KB NODE_DEV
eslint-plugin-react-hooks.production.min.js 🔺+0.6% 🔺+0.7% 23.31 KB 23.46 KB 7.89 KB 7.94 KB NODE_PROD

Size changes (stable)

Generated by 🚫 dangerJS against 65aaacf

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