Skip to content

Commit 0077454

Browse files
Merge pull request #22 from spatie/feature/react-flare-error-boundary-callback-improvements
add error to onReset and pass componentStack
2 parents 996f56d + 81b7c4f commit 0077454

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

.claude/docs/react-improvements.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,5 @@ For the `@flareapp/react` package specifically:
102102
- [x] FlareErrorBoundary supports resetKeys property
103103
- [x] FlareErrorBoundary supports fallback with a reset method for resetting the Error Boundary
104104
- [x] FlareErrorBoundary supports beforeCapture callback
105+
- [x] FlareErrorBoundary onReset passes previous error
106+
- [x] FlareErrorBoundary fallback passes componentStack

packages/react/src/FlareErrorBoundary.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { formatComponentStack } from './format-component-stack';
55

66
export type FlareErrorBoundaryFallbackProps = {
77
error: Error;
8+
componentStack: string[];
89
resetErrorBoundary: () => void;
910
};
1011

@@ -13,17 +14,18 @@ export type FlareErrorBoundaryProps = PropsWithChildren<{
1314
resetKeys?: unknown[];
1415
beforeCapture?: (params: { error: Error; errorInfo: ErrorInfo }) => void;
1516
onError?: (params: { error: Error; errorInfo: ErrorInfo }) => void;
16-
onReset?: () => void;
17+
onReset?: (error: Error | null) => void;
1718
}>;
1819

1920
export type FlareErrorBoundaryState = {
2021
error: Error | null;
22+
componentStack: string[];
2123
};
2224

2325
export class FlareErrorBoundary extends Component<FlareErrorBoundaryProps, FlareErrorBoundaryState> {
24-
state: FlareErrorBoundaryState = { error: null };
26+
state: FlareErrorBoundaryState = { error: null, componentStack: [] };
2527

26-
static getDerivedStateFromError(error: Error): FlareErrorBoundaryState {
28+
static getDerivedStateFromError(error: Error): Partial<FlareErrorBoundaryState> {
2729
return { error };
2830
}
2931

@@ -39,6 +41,8 @@ export class FlareErrorBoundary extends Component<FlareErrorBoundaryProps, Flare
3941
},
4042
};
4143

44+
this.setState({ componentStack: context.react.componentStack });
45+
4246
flare.report(error, context, { react: { errorInfo } });
4347

4448
this.props.onError?.({
@@ -64,9 +68,11 @@ export class FlareErrorBoundary extends Component<FlareErrorBoundaryProps, Flare
6468
}
6569

6670
reset = () => {
67-
this.setState({ error: null });
71+
const { error } = this.state;
72+
73+
this.props.onReset?.(error);
6874

69-
this.props.onReset?.();
75+
this.setState({ error: null, componentStack: [] });
7076
};
7177

7278
render() {
@@ -78,6 +84,7 @@ export class FlareErrorBoundary extends Component<FlareErrorBoundaryProps, Flare
7884
if (typeof fallback === 'function') {
7985
return fallback({
8086
error,
87+
componentStack: this.state.componentStack,
8188
resetErrorBoundary: this.reset,
8289
});
8390
}

0 commit comments

Comments
 (0)