From 2cf6c6d29e01f5e7793a3e0fe01e5d678c10a432 Mon Sep 17 00:00:00 2001 From: Innei Date: Fri, 12 Aug 2022 11:16:16 +0800 Subject: [PATCH] fix: toc depth Signed-off-by: Innei --- src/components/app/ErrorBoundary/index.tsx | 35 ++++++++++++++++++++ src/components/layouts/BasicLayout/index.tsx | 1 + src/components/widgets/Toc/index.tsx | 10 +++--- src/components/widgets/Toc/item.tsx | 33 ++++++++++-------- 4 files changed, 62 insertions(+), 17 deletions(-) create mode 100644 src/components/app/ErrorBoundary/index.tsx 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}
+