errorboundary dy dy not of capture错误时,当简单组件调用词典中哪些错误

发布于 2025-02-14 00:44:51 字数 1646 浏览 0 评论 0原文

我在

应该出错的简单组件,但部分的部分仍应呈现:

export const App = () => {
    return 
          <ErrorBoundary>
            <Suspense fallback={<LoadingIcon/>}>
              <div>this should show #1</div>
              <ErrorBoundary>
               <SimpleComponent />
              </ErrorBoundary>
            </Suspense>
        </ErrorBoundary>
});


export const SimpleComponent: FC = ({}) => {
    const mydictionary: Dictionary<() => JSX.Element> = {};

    return (
        <div>
           stuff that should still render. #2
            <ErrorBoundary>
                <div>{mydictionary['nonexistentvalue']()}</div>
            </ErrorBoundary>
        </div>
    );
};

错误边界:

export class ErrorBoundary extends React.Component<{}, { hasError: any }> {
    constructor(props: any) {
        super(props);
        this.state = { hasError: false };
    }

    static getDerivedStateFromError(error: any) {
        // Update state so the next render will show the fallback UI.
        console.log('err', error);
        return { hasError: true };
    }

    componentDidCatch(error: any, errorInfo: any) {
        this.setState({ hasError: true });
        console.error('Help', error, errorInfo);
    }

    render() {
        if (this.state.hasError) {
            // You can render any custom fallback UI
            return <h1>Something went wrong here.</h1>;
        }

        return this.props.children;
    }
}

我得到的只是单个在这里出现问题。在页面上。

I have the default error boundary mentioned in the react docs and I have a dictionary that would normally be filled with functions I can execute.
I've specifically created a test case below which will error out because a particular function doesn't exist in the dictionary, I'm expecting the error boundary to catch this, however, it doesn't appear to work, what might be going on?

The simple component that should error out, but parts of it should still render:

export const App = () => {
    return 
          <ErrorBoundary>
            <Suspense fallback={<LoadingIcon/>}>
              <div>this should show #1</div>
              <ErrorBoundary>
               <SimpleComponent />
              </ErrorBoundary>
            </Suspense>
        </ErrorBoundary>
});


export const SimpleComponent: FC = ({}) => {
    const mydictionary: Dictionary<() => JSX.Element> = {};

    return (
        <div>
           stuff that should still render. #2
            <ErrorBoundary>
                <div>{mydictionary['nonexistentvalue']()}</div>
            </ErrorBoundary>
        </div>
    );
};

The error boundary:

export class ErrorBoundary extends React.Component<{}, { hasError: any }> {
    constructor(props: any) {
        super(props);
        this.state = { hasError: false };
    }

    static getDerivedStateFromError(error: any) {
        // Update state so the next render will show the fallback UI.
        console.log('err', error);
        return { hasError: true };
    }

    componentDidCatch(error: any, errorInfo: any) {
        this.setState({ hasError: true });
        console.error('Help', error, errorInfo);
    }

    render() {
        if (this.state.hasError) {
            // You can render any custom fallback UI
            return <h1>Something went wrong here.</h1>;
        }

        return this.props.children;
    }
}

What I get is just the single Something went wrong here. on the page.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

已下线请稍等 2025-02-21 00:44:51

尝试将您的SimpleComponent与您的errorboundary组件包装。当SimpleComponent将被包装错误的错误边界组件应正确工作。 Docs说,错误边界组件只能处理用此错误边界组件包裹的儿童组件的问题。在同一级别上,它行不通。

<ErrorBoundary>
  <SimpleComponent/>
</ErrorBoundary>

Try to wrap your SimpleComponent with your ErrorBoundary component. When SimpleComponent will be wrapped error boundary component should work correctly. Docs says that error boundary component can handle only issues of children components wrapped with this error boundary component. On the same level it won't work.

<ErrorBoundary>
  <SimpleComponent/>
</ErrorBoundary>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文