Skip to content

Commit 33f3471

Browse files
committed
Also componentDidCatch
1 parent 2e796ea commit 33f3471

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

packages/react-reconciler/src/ReactFiberCallUserSpace.js

+25
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import type {Fiber} from './ReactInternalTypes';
1111
import type {LazyComponent} from 'react/src/ReactLazy';
1212
import type {Effect} from './ReactFiberHooks';
13+
import type {CapturedValue} from './ReactCapturedValue';
1314

1415
import {isRendering, setIsRendering} from './ReactCurrentFiber';
1516
import {captureCommitPhaseError} from './ReactFiberWorkLoop';
@@ -51,6 +52,7 @@ interface ClassInstance<R> {
5152
prevState: Object,
5253
snaphot: Object,
5354
): void;
55+
componentDidCatch(error: mixed, errorInfo: {componentStack: string}): void;
5456
componentWillUnmount(): void;
5557
}
5658

@@ -125,6 +127,29 @@ export const callComponentDidUpdateInDEV: (
125127
): any)
126128
: (null: any);
127129

130+
const callComponentDidCatch = {
131+
'react-stack-bottom-frame': function (
132+
instance: ClassInstance<any>,
133+
errorInfo: CapturedValue<mixed>,
134+
): void {
135+
const error = errorInfo.value;
136+
const stack = errorInfo.stack;
137+
instance.componentDidCatch(error, {
138+
componentStack: stack !== null ? stack : '',
139+
});
140+
},
141+
};
142+
143+
export const callComponentDidCatchInDEV: (
144+
instance: ClassInstance<any>,
145+
errorInfo: CapturedValue<mixed>,
146+
) => void = __DEV__
147+
? // We use this technique to trick minifiers to preserve the function name.
148+
(callComponentDidCatch['react-stack-bottom-frame'].bind(
149+
callComponentDidCatch,
150+
): any)
151+
: (null: any);
152+
128153
const callComponentWillUnmount = {
129154
'react-stack-bottom-frame': function (
130155
current: Fiber,

packages/react-reconciler/src/ReactFiberThrow.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ import {ConcurrentRoot} from './ReactRootTags';
8888
import {noopSuspenseyCommitThenable} from './ReactFiberThenable';
8989
import {REACT_POSTPONE_TYPE} from 'shared/ReactSymbols';
9090
import {runWithFiberInDEV} from './ReactCurrentFiber';
91+
import {callComponentDidCatchInDEV} from './ReactFiberCallUserSpace';
9192

9293
function createRootErrorUpdate(
9394
root: FiberRoot,
@@ -172,11 +173,15 @@ function initializeClassErrorUpdate(
172173
// not defined.
173174
markLegacyErrorBoundaryAsFailed(this);
174175
}
175-
const error = errorInfo.value;
176-
const stack = errorInfo.stack;
177-
this.componentDidCatch(error, {
178-
componentStack: stack !== null ? stack : '',
179-
});
176+
if (__DEV__) {
177+
callComponentDidCatchInDEV(this, errorInfo);
178+
} else {
179+
const error = errorInfo.value;
180+
const stack = errorInfo.stack;
181+
this.componentDidCatch(error, {
182+
componentStack: stack !== null ? stack : '',
183+
});
184+
}
180185
if (__DEV__) {
181186
if (typeof getDerivedStateFromError !== 'function') {
182187
// If componentDidCatch is the only error boundary method defined,

0 commit comments

Comments
 (0)