-
Notifications
You must be signed in to change notification settings - Fork 47k
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
Add hook warning when going to/from 0 hooks #24535
Conversation
packages/react-reconciler/src/__tests__/ReactHooksWithNoopRenderer-test.js
Show resolved
Hide resolved
Comparing: 229c86a...b5e48aa 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
|
This actually makes it throw, right? Not just a warning. So this would hard-break the code that currently (accidentally) relies on this. I was thinking we'd do it the opposite way instead. I.e. fix the warning first to always show up. And later make it throw consistently after people have had a chance to fix the warnings. |
7cc97ff
to
03f1089
Compare
@gaearon that makes sense, I can update that. |
4f4fc4e
to
23a3ad7
Compare
Ok I've updated the logic here such that:
|
6e92003
to
4b511ab
Compare
4b511ab
to
b5e48aa
Compare
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.
Ready to review
@@ -478,11 +521,25 @@ export function renderWithHooks<Props, SecondArg>( | |||
ReactCurrentDispatcher.current = ContextOnlyDispatcher; | |||
|
|||
if (__DEV__) { | |||
if ( |
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.
Calling this out - this isn't pretty but it adds the warning for going from some hooks to no hooks.
'Rendered fewer hooks than expected. This may be caused by an ' + | ||
'accidental early return statement.', | ||
); | ||
}).toErrorDev([ |
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 the warning the not pretty code mentioned above adds. As well as the "unmount effect" test below.
'Rendered fewer hooks than expected. This may be caused by an ' + | ||
'accidental early return statement.', | ||
); | ||
}).toErrorDev([ |
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 the warning the not pretty code mentioned above adds. As well as the "unmount effect" test below.
Replaced by: #25216 |
Overview
Add a hook warning for when we go from 0 to n > 0 hooks or from n > 0 to 0 hooks in a component, such as using an early return like:
We only add this warning in the new root, since the blocker to this in legacy mode was a quirk with React.lazy not applicable in the new root.