Skip to content

Commit

Permalink
Also componentDidCatch
Browse files Browse the repository at this point in the history
  • Loading branch information
sebmarkbage committed Jul 23, 2024
1 parent 2e796ea commit 33f3471
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
25 changes: 25 additions & 0 deletions packages/react-reconciler/src/ReactFiberCallUserSpace.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import type {Fiber} from './ReactInternalTypes';
import type {LazyComponent} from 'react/src/ReactLazy';
import type {Effect} from './ReactFiberHooks';
import type {CapturedValue} from './ReactCapturedValue';

import {isRendering, setIsRendering} from './ReactCurrentFiber';
import {captureCommitPhaseError} from './ReactFiberWorkLoop';
Expand Down Expand Up @@ -51,6 +52,7 @@ interface ClassInstance<R> {
prevState: Object,
snaphot: Object,
): void;
componentDidCatch(error: mixed, errorInfo: {componentStack: string}): void;
componentWillUnmount(): void;
}

Expand Down Expand Up @@ -125,6 +127,29 @@ export const callComponentDidUpdateInDEV: (
): any)
: (null: any);

const callComponentDidCatch = {
'react-stack-bottom-frame': function (
instance: ClassInstance<any>,
errorInfo: CapturedValue<mixed>,
): void {
const error = errorInfo.value;
const stack = errorInfo.stack;
instance.componentDidCatch(error, {
componentStack: stack !== null ? stack : '',
});
},
};

export const callComponentDidCatchInDEV: (
instance: ClassInstance<any>,
errorInfo: CapturedValue<mixed>,
) => void = __DEV__
? // We use this technique to trick minifiers to preserve the function name.
(callComponentDidCatch['react-stack-bottom-frame'].bind(
callComponentDidCatch,
): any)
: (null: any);

const callComponentWillUnmount = {
'react-stack-bottom-frame': function (
current: Fiber,
Expand Down
15 changes: 10 additions & 5 deletions packages/react-reconciler/src/ReactFiberThrow.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ import {ConcurrentRoot} from './ReactRootTags';
import {noopSuspenseyCommitThenable} from './ReactFiberThenable';
import {REACT_POSTPONE_TYPE} from 'shared/ReactSymbols';
import {runWithFiberInDEV} from './ReactCurrentFiber';
import {callComponentDidCatchInDEV} from './ReactFiberCallUserSpace';

function createRootErrorUpdate(
root: FiberRoot,
Expand Down Expand Up @@ -172,11 +173,15 @@ function initializeClassErrorUpdate(
// not defined.
markLegacyErrorBoundaryAsFailed(this);
}
const error = errorInfo.value;
const stack = errorInfo.stack;
this.componentDidCatch(error, {
componentStack: stack !== null ? stack : '',
});
if (__DEV__) {
callComponentDidCatchInDEV(this, errorInfo);
} else {
const error = errorInfo.value;
const stack = errorInfo.stack;
this.componentDidCatch(error, {
componentStack: stack !== null ? stack : '',
});
}
if (__DEV__) {
if (typeof getDerivedStateFromError !== 'function') {
// If componentDidCatch is the only error boundary method defined,
Expand Down

0 comments on commit 33f3471

Please sign in to comment.