admin管理员组文章数量:1355654
I am trying to create an error boundary but it not working. The ponentDidCatch not firing.
I expected that the fallback UI will render but actually the original UI is still there and nothing is happening.
const SimpleBtn = lazy(() => import("./ponentJsx/SimpleBtn.jsx"));
const SimpleBtn2 = lazy(() => import("./ponentJsx/SimpleBtn2.jsx"));
class ErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = { error: "", errorInfo: "" };
}
static getDerivedStateFromError(error) {
return error;
}
ponentDidCatch(error, errorInfo) {
this.setState({ error, errorInfo });
console.log(error, errorInfo);
}
render() {
return this.state.errorInfo ? (
<>
<h3>Something is wrong!</h3>
<details>{this.state.error}</details>
</>
) : (
this.props.children
);
}
}
class App extends React.Component {
constructor(props) {
super(props);
this.state = { toggle: false, hasError: false };
}
toggleBtn = () => {
// use transition
startTransition(() => {
this.setState((state) => ({
toggle: !state.toggle,
}));
});
};
render() {
const { toggle } = this.state;
return (
<ErrorBoundary>
<Suspense fallback={<div>...Loading</div>}>
<h1> Sample file </h1>
{toggle && <SimpleBtn toggleBtn={this.toggleBtn} />}
<SimpleBtn2 toggleBtn={this.toggleBtn} />
<button
onClick={() => {
throw new Error("i am error!");
}}
>
Throw Error
</button>
</Suspense>
</ErrorBoundary>
);
}
}
I found a similar question but it not answered.
I tried to write code as in official react documentation but still not working and i can't figure out the problem also!.
After using Components tab i got to know that the error state is not changing.
I am trying to create an error boundary but it not working. The ponentDidCatch not firing.
I expected that the fallback UI will render but actually the original UI is still there and nothing is happening.
const SimpleBtn = lazy(() => import("./ponentJsx/SimpleBtn.jsx"));
const SimpleBtn2 = lazy(() => import("./ponentJsx/SimpleBtn2.jsx"));
class ErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = { error: "", errorInfo: "" };
}
static getDerivedStateFromError(error) {
return error;
}
ponentDidCatch(error, errorInfo) {
this.setState({ error, errorInfo });
console.log(error, errorInfo);
}
render() {
return this.state.errorInfo ? (
<>
<h3>Something is wrong!</h3>
<details>{this.state.error}</details>
</>
) : (
this.props.children
);
}
}
class App extends React.Component {
constructor(props) {
super(props);
this.state = { toggle: false, hasError: false };
}
toggleBtn = () => {
// use transition
startTransition(() => {
this.setState((state) => ({
toggle: !state.toggle,
}));
});
};
render() {
const { toggle } = this.state;
return (
<ErrorBoundary>
<Suspense fallback={<div>...Loading</div>}>
<h1> Sample file </h1>
{toggle && <SimpleBtn toggleBtn={this.toggleBtn} />}
<SimpleBtn2 toggleBtn={this.toggleBtn} />
<button
onClick={() => {
throw new Error("i am error!");
}}
>
Throw Error
</button>
</Suspense>
</ErrorBoundary>
);
}
}
I found a similar question but it not answered.
I tried to write code as in official react documentation but still not working and i can't figure out the problem also!.
After using Components tab i got to know that the error state is not changing.
Share Improve this question edited Jul 15, 2022 at 12:06 yashlodhi asked Jul 15, 2022 at 11:21 yashlodhiyashlodhi 1391 silver badge15 bronze badges2 Answers
Reset to default 7This is because error boundaries only work for errors thrown during render. If you throw an error in a ponent render function instead it will work.
If you want to capture errors in event handlers you'll need to register an error handler on the window or do local error handling.
If youre just trying to test your error boundary throw in a ponent render instead and conditionally load that ponent if you like.
See https://reactjs/docs/error-boundaries.html#how-about-event-handlers
According to the react doc "Error boundaries do not catch errors inside event handlers."
If you want to trigger an error inside an error boundary, you can use the following example:
function ErrorExample() {
throw new Error("Test Error");
return <div>Your Broken Component</div>;
}
// Render this inside an error boundary
<ErrorBoundary>
<ErrorExample />
</ErrorBoundary>
This will be caught by the ErrorBoundary
ponent since it happens during rendering. However, if an error occurs inside an event handler, it won’t be caught by the error boundary. If you need to globally catch errors inside event handlers you must handle them manually on the global error
event listener, see example below
window.addEventListener('error', (event) => {
console.error("Global error caught:", event.error);
});
本文标签: javascriptError boundary fallback ui not rendering in reactStack Overflow
版权声明:本文标题:javascript - Error boundary fallback ui not rendering in react? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743952702a2567580.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论