errorboundary dy dy not of capture错误时,当简单组件调用词典中哪些错误
我在
应该出错的简单组件,但部分的部分仍应呈现:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将您的
SimpleComponent
与您的errorboundary
组件包装。当SimpleComponent
将被包装错误的错误边界组件应正确工作。 Docs说,错误边界组件只能处理用此错误边界组件包裹的儿童组件的问题。在同一级别上,它行不通。Try to wrap your
SimpleComponent
with yourErrorBoundary
component. WhenSimpleComponent
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.