React 错误边界不适用于 React
React Error Boundaries not working with React
这是我的错误边界文件 -
class ErrorHandling extends Component {
state = { hasError: false }
componentDidCatch() {
this.setState({ hasError: true })
}
render() {
// debugger
if (this.state.hasError) {
return <div>Error in Component</div>
}
return this.props.children
}
}
而另一个文件是-
import React, { Component } from 'react';
// Intentionally I have added syntax error below 'd'
function Intermediate(props) {
return <h1>hi</h1>;d
}
export default Intermediate
在我的 App.js
<ErrorHandling>
<Intermediate />
</ErrorHandling>
它导致应用程序在未捕获错误的情况下中断。这是在浏览器屏幕上看到的错误
更详细的版本在这里-
https://codepen.io/meghana1991/pen/abojydj?editors=0010
当我在本地使用相同的代码处理上面列出的多个文件时,它不起作用
您无法捕获编译时错误,Error Boundaries 用于 UI.
中的 运行 时错误
参考Compile time vs run time errors.
此外,您必须 use getDerivedStateFromError
才能在回退时添加额外的渲染 UI:
class ErrorBoundary extends React.Component {
state = {
hasError: false,
error: { message: '', stack: '' },
info: { componentStack: '' }
};
static getDerivedStateFromError = error => {
return { hasError: true };
};
componentDidCatch = (error, info) => {
this.setState({ error, info });
};
render() {
const { hasError, error, info } = this.state;
const { children } = this.props;
return hasError ? <ErrorComponent/> : children;
}
}
为了检查您的 ErrorBoundary
,从组件树中的 a reachable 部分抛出错误 which is not:
- 事件处理程序
- 异步代码(例如 setTimeout 或 requestAnimationFrame 回调)
- 服务器端渲染
- 在错误边界本身(而不是它的子边界)中抛出的错误
const ButtonComponent = () => {
throw Error("error!");
return <></>;
};
Note: In development env you will always see the error overlay unless you turn it off or close it with the X button.
完整示例:
const ErrorComponent = () => {
return <h1>Something went wrong</h1>;
};
class ErrorBoundary extends React.Component {
state = {
hasError: false,
error: { message: "", stack: "" },
info: { componentStack: "" }
};
static getDerivedStateFromError = error => {
return { hasError: true };
};
componentDidCatch = (error, info) => {
this.setState({ error, info });
};
render() {
const { hasError, error, info } = this.state;
console.log(error, info);
const { children } = this.props;
return hasError ? <ErrorComponent /> : children;
}
}
const ButtonComponent = () => {
throw Error("error!");
return <></>;
};
const App = () => {
return (
<ErrorBoundary>
<ButtonComponent />
</ErrorBoundary>
);
};
问题是:您在 React 文档示例中看到的漂亮回退 UI 只出现在 生产中 。所以你必须 运行 create-react-app
建议的(你使用它)命令:
npm run build
# wait for it to finish
serve -s build
然后在浏览器中打开 localhost:5000
(如果您在提到的终端中看到此地址)。这样 React 文档示例就可以正常工作了。
这是我的错误边界文件 -
class ErrorHandling extends Component {
state = { hasError: false }
componentDidCatch() {
this.setState({ hasError: true })
}
render() {
// debugger
if (this.state.hasError) {
return <div>Error in Component</div>
}
return this.props.children
}
}
而另一个文件是-
import React, { Component } from 'react';
// Intentionally I have added syntax error below 'd'
function Intermediate(props) {
return <h1>hi</h1>;d
}
export default Intermediate
在我的 App.js
<ErrorHandling>
<Intermediate />
</ErrorHandling>
它导致应用程序在未捕获错误的情况下中断。这是在浏览器屏幕上看到的错误
更详细的版本在这里- https://codepen.io/meghana1991/pen/abojydj?editors=0010
当我在本地使用相同的代码处理上面列出的多个文件时,它不起作用
您无法捕获编译时错误,Error Boundaries 用于 UI.
中的 运行 时错误参考Compile time vs run time errors.
此外,您必须 use getDerivedStateFromError
才能在回退时添加额外的渲染 UI:
class ErrorBoundary extends React.Component {
state = {
hasError: false,
error: { message: '', stack: '' },
info: { componentStack: '' }
};
static getDerivedStateFromError = error => {
return { hasError: true };
};
componentDidCatch = (error, info) => {
this.setState({ error, info });
};
render() {
const { hasError, error, info } = this.state;
const { children } = this.props;
return hasError ? <ErrorComponent/> : children;
}
}
为了检查您的 ErrorBoundary
,从组件树中的 a reachable 部分抛出错误 which is not:
- 事件处理程序
- 异步代码(例如 setTimeout 或 requestAnimationFrame 回调)
- 服务器端渲染
- 在错误边界本身(而不是它的子边界)中抛出的错误
const ButtonComponent = () => {
throw Error("error!");
return <></>;
};
Note: In development env you will always see the error overlay unless you turn it off or close it with the X button.
完整示例:
const ErrorComponent = () => {
return <h1>Something went wrong</h1>;
};
class ErrorBoundary extends React.Component {
state = {
hasError: false,
error: { message: "", stack: "" },
info: { componentStack: "" }
};
static getDerivedStateFromError = error => {
return { hasError: true };
};
componentDidCatch = (error, info) => {
this.setState({ error, info });
};
render() {
const { hasError, error, info } = this.state;
console.log(error, info);
const { children } = this.props;
return hasError ? <ErrorComponent /> : children;
}
}
const ButtonComponent = () => {
throw Error("error!");
return <></>;
};
const App = () => {
return (
<ErrorBoundary>
<ButtonComponent />
</ErrorBoundary>
);
};
问题是:您在 React 文档示例中看到的漂亮回退 UI 只出现在 生产中 。所以你必须 运行 create-react-app
建议的(你使用它)命令:
npm run build
# wait for it to finish
serve -s build
然后在浏览器中打开 localhost:5000
(如果您在提到的终端中看到此地址)。这样 React 文档示例就可以正常工作了。