-
Notifications
You must be signed in to change notification settings - Fork 46.3k
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
Fix state leaking when a function component throws on server render #19212
Fix state leaking when a function component throws on server render #19212
Conversation
Hi @pmaccart! Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. If you have received this in error or have any questions, please contact us at [email protected]. Thanks! |
Can you add a test that would have been failing before? That would make it easier to check the changes. |
Can we instead add |
This comment has been minimized.
This comment has been minimized.
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 21fdac0:
|
6dad98b
to
46556a4
Compare
46556a4
to
398116c
Compare
@gaearon Thanks for the feedback, and sorry for the slow response. I made a couple updates to the PR:
|
packages/react-dom/src/__tests__/ReactDOMServerIntegrationHooks-test.js
Outdated
Show resolved
Hide resolved
if (__DEV__) { | ||
isInHookUserCodeInDev = false; | ||
} | ||
|
||
// These were reset above | ||
// These were reset via the resetHooksState() call |
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.
Let's just remove this comment?
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.
Done.
numberOfReRenders = 0; | ||
renderPhaseUpdates = null; | ||
workInProgressHook = null; | ||
resetHooksState(); | ||
if (__DEV__) { |
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.
Move this DEV-only block to reset too?
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.
Done.
@@ -202,24 +202,21 @@ export function finishHooks( | |||
|
|||
children = Component(props, refOrContext); | |||
} | |||
currentlyRenderingComponent = null; |
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.
Did we lose currentlyRenderingComponent?
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.
Oops, yes -- added that in as well.
Looks good. Thx |
…acebook#19212) * add unit test asserting internal hooks state is reset * Reset internal hooks state before rendering * reset hooks state on error * Use expect...toThrow instead of try/catch in test * reset dev-only hooks state inside resetHooksState * reset currentlyRenderingComponent to null
Summary
Reset the internal hooks state when preparing a component for hooks usage, rather than after a component finishes rendering. This fixes #19211, where the internal hooks state is not cleared if a function component throws an error while rendering.
Test Plan
yarn test-prod
: validated no test regressions were introduced