Skip to content
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
Fix bug when fatal error is thrown as a result of batch.commit
Fixes #12474
  • Loading branch information
acdlite committed Mar 29, 2018
commit c21ed5a3ca43d588cf925fb1ce5cfed28770715a
10 changes: 10 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOMRoot-test.internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,4 +320,14 @@ describe('ReactDOMRoot', () => {
flush();
expect(container.textContent).toEqual('1');
});

it('handles fatal errors triggered by batch.commit()', () => {
const root = ReactDOM.createRoot(container);
const batch = root.createBatch();
const InvalidType = undefined;
expect(() => batch.render(<InvalidType />)).toWarnDev([
'React.createElement: type is invalid',
]);
expect(() => batch.commit()).toThrow('Element type is invalid');
});
});
9 changes: 8 additions & 1 deletion packages/react-reconciler/src/ReactFiberScheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,12 @@ export default function<T, P, I, TI, HI, PI, C, CC, CX, PL>(
const sourceFiber: Fiber = nextUnitOfWork;
let returnFiber = sourceFiber.return;
if (returnFiber === null) {
// This is a fatal error.
// This is the root. The root could capture its own errors. However,
// we don't know if it errors before or after we pushed the host
// context. This information is needed to avoid a stack mismatch.
// Because we're not sure, treat this as a fatal error. We could track
// which phase it fails in, but doesn't seem worth it. At least
// for now.
didFatal = true;
onUncaughtError(thrownValue);
break;
Expand Down Expand Up @@ -1444,6 +1449,8 @@ export default function<T, P, I, TI, HI, PI, C, CC, CX, PL>(
// Perform work on root as if the given expiration time is the current time.
// This has the effect of synchronously flushing all work up to and
// including the given time.
nextFlushedRoot = root;
nextFlushedExpirationTime = expirationTime;
performWorkOnRoot(root, expirationTime, false);
finishRendering();
}
Expand Down