diff --git a/src/components/app/ErrorBoundary/index.tsx b/src/components/app/ErrorBoundary/index.tsx new file mode 100644 index 000000000..0d88d70c0 --- /dev/null +++ b/src/components/app/ErrorBoundary/index.tsx @@ -0,0 +1,35 @@ +import React, { Component } from 'react' + +export class ErrorBoundary extends Component<{ + children: React.ReactNode + [k: string]: any +}> { + state: any = { + error: null, + errorInfo: null, + } + + componentDidCatch(error, errorInfo) { + console.error(error, 'render error') + console.error(errorInfo) + + this.setState({ + error, + errorInfo, + }) + } + + render() { + const { errorInfo } = this.state + const { children, ...restProps } = this.props + + if (errorInfo) { + return
渲染报错
+ } + + // @ts-ignore + return React.cloneElement(children, { + ...restProps, + }) + } +} diff --git a/src/components/layouts/BasicLayout/index.tsx b/src/components/layouts/BasicLayout/index.tsx index 9a5e7af63..7095059a3 100644 --- a/src/components/layouts/BasicLayout/index.tsx +++ b/src/components/layouts/BasicLayout/index.tsx @@ -133,6 +133,7 @@ export const BasicLayout: FC = observer(({ children }) => {
{children}
+