Skip to content

Commit 705d2e9

Browse files
author
Sébastien Henau
committed
add reset properties and functionality to the error boundary
1 parent 64cde0b commit 705d2e9

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

.claude/docs/react-improvements.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,7 @@ For the `@flareapp/react` package specifically:
9797
## Tasks
9898

9999
- [x] FlareErrorBoundary supports onError callback
100+
- [x] FlareErrorBoundary supports fallback property
101+
- [x] FlareErrorBoundary supports onReset property
102+
- [x] FlareErrorBoundary supports resetKeys property
103+
- [x] FlareErrorBoundary supports fallback with a reset method for reseting the Error Boundary

packages/react/src/FlareErrorBoundary.ts

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

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

1011
export type FlareErrorBoundaryProps = PropsWithChildren<{
1112
fallback?: ReactNode | ((props: FlareErrorBoundaryFallbackProps) => ReactNode);
13+
resetKeys?: unknown[];
1214
onError?: (error: Error, errorInfo: ErrorInfo) => void;
15+
onReset?: () => void;
1316
}>;
1417

1518
export type FlareErrorBoundaryState = {
@@ -35,6 +38,28 @@ export class FlareErrorBoundary extends Component<FlareErrorBoundaryProps, Flare
3538
this.props.onError?.(error, errorInfo);
3639
}
3740

41+
componentDidUpdate(prevProps: FlareErrorBoundaryProps) {
42+
if (this.state.error === null || !this.props.resetKeys) {
43+
return;
44+
}
45+
46+
const prevKeys = prevProps.resetKeys;
47+
const nextKeys = this.props.resetKeys;
48+
49+
const lengthChanged = prevKeys?.length !== nextKeys.length;
50+
const valuesChanged = nextKeys.some((key, i) => !Object.is(key, prevKeys?.[i]));
51+
52+
if (lengthChanged || valuesChanged) {
53+
this.reset();
54+
}
55+
}
56+
57+
reset = () => {
58+
this.setState({ error: null });
59+
60+
this.props.onReset?.();
61+
};
62+
3863
render() {
3964
const { error } = this.state;
4065

@@ -44,6 +69,7 @@ export class FlareErrorBoundary extends Component<FlareErrorBoundaryProps, Flare
4469
if (typeof fallback === 'function') {
4570
return fallback({
4671
error,
72+
resetErrorBoundary: this.reset,
4773
});
4874
}
4975

0 commit comments

Comments
 (0)