-
Notifications
You must be signed in to change notification settings - Fork 45.5k
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
react-hooks: Rules of Hooks now considers component/hook declarations inside JSX attributes #25440
Conversation
code: normalizeIndent` | ||
function App() { | ||
return createElement(Child, { | ||
Component: () => { |
There was a problem hiding this comment.
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
node.parent.type === 'JSXExpressionContainer' && | ||
node.parent.parent.type === 'JSXAttribute' && | ||
node.parent.parent.name.type === 'JSXIdentifier' |
There was a problem hiding this comment.
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 😉 )
Comparing: bdd3d08...ff90b22 Critical size changesIncludes critical production bundles, as well as any change greater than 2%:
Significant size changesIncludes any change greater than 0.2%: Expand to show
|
66245d6
to
fe1e889
Compare
fe1e889
to
4a482ea
Compare
return ( | ||
<div className="App"> | ||
<Foo useData={async () => useQuery()} /> | ||
<Foo useData={useNamed} /> |
There was a problem hiding this comment.
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
4a482ea
to
ff90b22
Compare
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. |
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! |
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.
The above code is equivalent to the code below where we already apply Rules of Hooks
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?