@@ -5,11 +5,14 @@ import { formatComponentStack } from './format-component-stack';
55
66export type FlareErrorBoundaryFallbackProps = {
77 error : Error ;
8+ resetErrorBoundary : ( ) => void ;
89} ;
910
1011export type FlareErrorBoundaryProps = PropsWithChildren < {
1112 fallback ?: ReactNode | ( ( props : FlareErrorBoundaryFallbackProps ) => ReactNode ) ;
13+ resetKeys ?: unknown [ ] ;
1214 onError ?: ( error : Error , errorInfo : ErrorInfo ) => void ;
15+ onReset ?: ( ) => void ;
1316} > ;
1417
1518export 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