Skip to content

Commit

Permalink
Fix bug when fatal error is thrown as a result of batch.commit (#12480
Browse files Browse the repository at this point in the history
)

Fixes #12474
  • Loading branch information
acdlite authored Mar 29, 2018
1 parent 268a3f6 commit c44665e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
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 @@ -932,7 +932,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 @@ -1492,6 +1497,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

0 comments on commit c44665e

Please sign in to comment.