@@ -2,14 +2,15 @@ import { TreeStore } from '@stoplight/tree-list';
22import * as cn from 'classnames' ;
33import { runInAction } from 'mobx' ;
44import * as React from 'react' ;
5- import ErrorBoundary , { ErrorBoundaryProps , FallbackProps } from 'react-error-boundary' ;
65
76import { JSONSchema4 } from 'json-schema' ;
87import { GoToRefHandler } from '../types' ;
98import { isSchemaViewerEmpty , renderSchema } from '../utils' ;
109import { SchemaTree } from './SchemaTree' ;
1110
12- export interface IJsonSchemaViewer extends ErrorBoundaryProps {
11+ export type FallbackComponent = React . ComponentType < { error : Error | null } > ;
12+
13+ export interface IJsonSchemaViewer {
1314 schema : JSONSchema4 ;
1415 dereferencedSchema ?: JSONSchema4 ;
1516 style ?: object ;
@@ -21,6 +22,7 @@ export interface IJsonSchemaViewer extends ErrorBoundaryProps {
2122 hideTopBar ?: boolean ;
2223 maxRows ?: number ;
2324 onGoToRef ?: GoToRefHandler ;
25+ FallbackComponent ?: FallbackComponent ;
2426}
2527
2628export class JsonSchemaViewerComponent extends React . PureComponent < IJsonSchemaViewer > {
@@ -86,7 +88,7 @@ export class JsonSchemaViewerComponent extends React.PureComponent<IJsonSchemaVi
8688 }
8789}
8890
89- const JsonSchemaFallbackComponent : React . FunctionComponent < FallbackProps > = ( { error } ) => {
91+ const JsonSchemaFallbackComponent : FallbackComponent = ( { error } ) => {
9092 return (
9193 < div className = "p-4" >
9294 < b > Error</ b >
@@ -95,15 +97,32 @@ const JsonSchemaFallbackComponent: React.FunctionComponent<FallbackProps> = ({ e
9597 ) ;
9698} ;
9799
98- export const JsonSchemaViewer : React . FunctionComponent < IJsonSchemaViewer > = ( {
99- onError,
100- FallbackComponent = JsonSchemaFallbackComponent ,
101- ...props
102- } ) => {
103- return (
104- < ErrorBoundary onError = { onError } FallbackComponent = { FallbackComponent } >
105- < JsonSchemaViewerComponent { ...props } />
106- </ ErrorBoundary >
107- ) ;
108- } ;
109- JsonSchemaViewer . displayName = 'JsonSchemaViewer' ;
100+ // react-error-boundary does not support recovering, see https://github.com/bvaughn/react-error-boundary/pull/16/files
101+ export class JsonSchemaViewer extends React . PureComponent < IJsonSchemaViewer , { error : null | Error } > {
102+ public state = {
103+ error : null ,
104+ } ;
105+
106+ public componentDidUpdate ( prevProps : Readonly < IJsonSchemaViewer > ) {
107+ if (
108+ this . state . error !== null &&
109+ ( prevProps . schema !== this . props . schema || prevProps . dereferencedSchema !== this . props . dereferencedSchema )
110+ ) {
111+ this . setState ( { error : null } ) ;
112+ }
113+ }
114+
115+ // there is no error hook yet, see https://reactjs.org/docs/hooks-faq.html#how-do-lifecycle-methods-correspond-to-hooks
116+ public static getDerivedStateFromError ( error : Error ) {
117+ return { error } ;
118+ }
119+
120+ public render ( ) {
121+ const { FallbackComponent : Fallback = JsonSchemaFallbackComponent , ...props } = this . props ;
122+ if ( this . state . error ) {
123+ return < Fallback error = { this . state . error } /> ;
124+ }
125+
126+ return < JsonSchemaViewerComponent { ...props } /> ;
127+ }
128+ }
0 commit comments