constinitialState: ErrorBoundaryState ={didCatch:false,// 错误是否捕捉error:null,// 捕捉到的错误信息};exportclassErrorBoundaryextendsComponent<
ErrorBoundaryProps,
ErrorBoundaryState
>{// ...resetErrorBoundary(...args: any[]){const{ error }=this.state;if(error !==null){this.props.onReset?.({// 触发对应回调
args,reason:"imperative-api",});this.setState(initialState);}}// ...// 根据 resetKeys 重置,但并未对外暴露该 APIcomponentDidUpdate(prevProps: ErrorBoundaryProps,prevState: ErrorBoundaryState){const{ didCatch }=this.state;const{ resetKeys }=this.props;// There's an edge case where if the thing that triggered the error happens to *also* be in the resetKeys array,// we'd end up resetting the error boundary immediately.// This would likely trigger a second error to be thrown.// So we make sure that we don't check the resetKeys on the first call of cDU after the error is set.if(
didCatch &&
prevState.error !==null&&hasArrayChanged(prevProps.resetKeys, resetKeys)){this.props.onReset?.({next: resetKeys,prev: prevProps.resetKeys,reason:"keys",});this.setState(initialState);}}}functionhasArrayChanged(a: any[]=[],b: any[]=[]){return(
a.length !== b.length || a.some((item, index)=>!Object.is(item, b[index])));}