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 all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,44 @@ 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>;
}

// First, render a component that will throw an error during a re-render triggered
// by a dispatch call.
expect(() => ReactDOMServer.renderToString(<ThrowingComponent />)).toThrow(
'Error from ThrowingComponent',
);

// 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.
const container = document.createElement('div');
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
Original file line number Diff line number Diff line change
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
24 changes: 11 additions & 13 deletions packages/react-dom/src/server/ReactPartialRendererHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,24 +202,22 @@ export function finishHooks(

children = Component(props, refOrContext);
}
resetHooksState();
return children;
}

// Reset the internal hooks state if an error occurs while rendering a component
export function resetHooksState(): void {
if (__DEV__) {
isInHookUserCodeInDev = false;
}

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.

didScheduleRenderPhaseUpdate = false;
firstWorkInProgressHook = null;
numberOfReRenders = 0;
renderPhaseUpdates = null;
workInProgressHook = null;
if (__DEV__) {
isInHookUserCodeInDev = false;
}

// These were reset above
// currentlyRenderingComponent = null;
// didScheduleRenderPhaseUpdate = false;
// firstWorkInProgressHook = null;
// numberOfReRenders = 0;
// renderPhaseUpdates = null;
// workInProgressHook = null;

return children;
}

function readContext<T>(
Expand Down