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: Rules of Hooks now considers component/hook declarations inside JSX attributes #25440

Conversation

eps1lon
Copy link
Collaborator

@eps1lon eps1lon commented Oct 6, 2022

Summary

Noticed during work in #25360.
Closes #23230

Currently, the below code will trigger rules of hooks with "hooks can only be called at the top level" despite the hook being called in something that looks like a component.

function JSXApp() {
  return (
    <Child
      Component={() => {
        const [myState, setMyState] = useState(null);
      }}
    />
  );
}

The above code is equivalent to the code below where we already apply Rules of Hooks

function App() {
  return createElement(Child, {
    Component: () => {
      const [myState, setMyState] = useState(null);
    },
  });
}

It's kind of neat to not apply rules of hook the components in JSX attributes since this can prevent declaration of nested components. But the error message wouldn't make sense and it would also flag component declarations in JSX elements that are not created during render.

The idea is to release this and #25360 together (though #25360 would need some rework if this PR is merged to also consider JSXAttributes).

How did you test this change?

code: normalizeIndent`
function App() {
return createElement(Child, {
Component: () => {
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 is already considered a component in the latest release. Just not when it's written as JSX

@eps1lon eps1lon requested review from poteto and gaearon October 6, 2022 06:00
Comment on lines +685 to +687
node.parent.type === 'JSXExpressionContainer' &&
node.parent.parent.type === 'JSXAttribute' &&
node.parent.parent.name.type === 'JSXIdentifier'
Copy link
Collaborator Author

@eps1lon eps1lon Oct 6, 2022

Choose a reason for hiding this comment

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

I think this is overly defensive in todays JSX. But just in case expressioncontainers will ever be able to appear elsewhere and JSXAttributes keys might be something other than an identifier (e.g. computed 😉 )

@sizebot
Copy link

sizebot commented Oct 6, 2022

Comparing: bdd3d08...ff90b22

Critical size changes

Includes critical production bundles, as well as any change greater than 2%:

Name +/- Base Current +/- gzip Base gzip Current gzip
oss-stable/react-dom/cjs/react-dom.production.min.js = 153.13 kB 153.13 kB = 48.84 kB 48.84 kB
oss-experimental/react-dom/cjs/react-dom.production.min.js = 155.05 kB 155.05 kB = 49.44 kB 49.44 kB
facebook-www/ReactDOM-prod.classic.js = 529.21 kB 529.21 kB = 94.48 kB 94.49 kB
facebook-www/ReactDOM-prod.modern.js = 514.47 kB 514.47 kB = 92.32 kB 92.32 kB
facebook-www/ReactDOMForked-prod.classic.js = 529.21 kB 529.21 kB = 94.49 kB 94.49 kB

Significant size changes

Includes any change greater than 0.2%:

Expand to show
Name +/- Base Current +/- gzip Base gzip Current gzip
oss-stable-semver/eslint-plugin-react-hooks/cjs/eslint-plugin-react-hooks.production.min.js +0.75% 26.61 kB 26.81 kB +0.48% 9.08 kB 9.13 kB
oss-stable/eslint-plugin-react-hooks/cjs/eslint-plugin-react-hooks.production.min.js +0.75% 26.61 kB 26.81 kB +0.48% 9.08 kB 9.13 kB
oss-experimental/eslint-plugin-react-hooks/cjs/eslint-plugin-react-hooks.production.min.js +0.73% 27.31 kB 27.51 kB +0.45% 9.24 kB 9.29 kB
oss-stable-semver/eslint-plugin-react-hooks/cjs/eslint-plugin-react-hooks.development.js +0.41% 92.46 kB 92.84 kB +0.46% 21.80 kB 21.90 kB
oss-stable/eslint-plugin-react-hooks/cjs/eslint-plugin-react-hooks.development.js +0.41% 92.46 kB 92.84 kB +0.46% 21.80 kB 21.90 kB
oss-experimental/eslint-plugin-react-hooks/cjs/eslint-plugin-react-hooks.development.js +0.41% 92.65 kB 93.04 kB +0.48% 21.83 kB 21.93 kB

Generated by 🚫 dangerJS against ff90b22

@eps1lon eps1lon force-pushed the fix/eslint-plugin-react-hooks/components-as-props branch from 66245d6 to fe1e889 Compare October 6, 2022 06:05
@eps1lon eps1lon changed the title react-hooks: Rules of Hooks now considers component declarations inside JSX attributes react-hooks: Rules of Hooks now considers component/hook declarations inside JSX attributes Oct 29, 2022
@eps1lon eps1lon force-pushed the fix/eslint-plugin-react-hooks/components-as-props branch from fe1e889 to 4a482ea Compare October 29, 2022 08:18
return (
<div className="App">
<Foo useData={async () => useQuery()} />
<Foo useData={useNamed} />
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 is already accepted on main but not <Foo useData={async () => useQuery()} />. Test taken from #23230

@eps1lon eps1lon force-pushed the fix/eslint-plugin-react-hooks/components-as-props branch from 4a482ea to ff90b22 Compare October 29, 2022 08:30
Copy link

This pull request has been automatically marked as stale. If this pull request is still relevant, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize reviewing it yet. Your contribution is very much appreciated.

@github-actions github-actions bot added the Resolution: Stale Automatically closed due to inactivity label Apr 10, 2024
Copy link

Closing this pull request after a prolonged period of inactivity. If this issue is still present in the latest release, please ask for this pull request to be reopened. Thank you!

@github-actions github-actions bot closed this Apr 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[DevTools Bug]: Inconsistent enforcement of using hooks in callbacks
3 participants