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

Fix state leaking when a function component throws on server render #19212

Merged
merged 6 commits into from Jul 8, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -867,6 +867,48 @@ describe('ReactDOMServerHooks', () => {
});
});

it('renders successfully after a component using hooks throws an error', () => {
function ThrowingComponent() {
const [value, dispatch] = useReducer((state, action) => {
return state + 1;
}, 0);

// throw an error if the count gets too high during the re-render phase
if (value >= 3) {
throw new Error('Error from ThrowingComponent');
} else {
// dispatch to trigger a re-render of the component
dispatch();
}

return <div>{value}</div>;
}

function NonThrowingComponent() {
const [count] = useState(0);
return <div>{count}</div>;
}

const container = document.createElement('div');

// First, render a component that will throw an error during a re-render triggered
// by a dispatch call.
try {
gaearon marked this conversation as resolved.
Show resolved Hide resolved
container.innerHTML = ReactDOMServer.renderToString(
<ThrowingComponent />,
);
} catch (e) {}
expect(container.children.length).toEqual(0);

// Next, assert that we can render a function component using hooks immediately
// after an error occurred, which indictates the internal hooks state has been
// reset.
container.innerHTML = ReactDOMServer.renderToString(
<NonThrowingComponent />,
);
expect(container.children[0].textContent).toEqual('0');
});

if (__EXPERIMENTAL__) {
describe('useOpaqueIdentifier', () => {
it('generates unique ids for server string render', async () => {
Expand Down
2 changes: 2 additions & 0 deletions packages/react-dom/src/server/ReactPartialRenderer.js
Expand Up @@ -60,6 +60,7 @@ import escapeTextForBrowser from './escapeTextForBrowser';
import {
prepareToUseHooks,
finishHooks,
resetHooksState,
Dispatcher,
currentPartialRenderer,
setCurrentPartialRenderer,
Expand Down Expand Up @@ -955,6 +956,7 @@ class ReactDOMServerRenderer {
} finally {
ReactCurrentDispatcher.current = prevDispatcher;
setCurrentPartialRenderer(prevPartialRenderer);
resetHooksState();
}
}

Expand Down
17 changes: 11 additions & 6 deletions packages/react-dom/src/server/ReactPartialRendererHooks.js
Expand Up @@ -202,16 +202,12 @@ export function finishHooks(

children = Component(props, refOrContext);
}
currentlyRenderingComponent = null;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Did we lose currentlyRenderingComponent?

Copy link
Contributor Author

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.

firstWorkInProgressHook = null;
numberOfReRenders = 0;
renderPhaseUpdates = null;
workInProgressHook = null;
resetHooksState();
if (__DEV__) {
Copy link
Collaborator

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

isInHookUserCodeInDev = false;
}

// These were reset above
// These were reset via the resetHooksState() call
Copy link
Collaborator

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

// currentlyRenderingComponent = null;
// didScheduleRenderPhaseUpdate = false;
// firstWorkInProgressHook = null;
Expand All @@ -222,6 +218,15 @@ export function finishHooks(
return children;
}

// Reset the internal hooks state if an error occurs while rendering a component
export function resetHooksState(): void {
didScheduleRenderPhaseUpdate = false;
firstWorkInProgressHook = null;
numberOfReRenders = 0;
renderPhaseUpdates = null;
workInProgressHook = null;
}

function readContext<T>(
context: ReactContext<T>,
observedBits: void | number | boolean,
Expand Down