【发布时间】:2019-02-20 02:59:13
【问题描述】:
我们有一个错误边界组件:
import React from 'react';
class ErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = { hasError: false, errorMessage:''};
}
componentDidCatch(error, info) {
// Display fallback UI
this.setState({ hasError: true, errorMessage: error });
// You can also log the error to an error reporting service
}
render() {
if (this.state.hasError) {
debugger
// You can render any custom fallback UI
return <h1>{this.state.errorMessage.toString()}</h1>;
}
return this.props.children;
}
}
export default ErrorBoundary;
我们是这样使用它的:
ReactDOM.render(
<Provider store={store}>
<Router>
<CookiesProvider>
<ErrorBoundary>
<NetworkMgmt />
</ErrorBoundary>
</CookiesProvider>
</Router>
</Provider>,
document.getElementById('root')
);
之后我尝试使用下面的代码 sn-p 来抛出错误,但这不会调用错误边界 componentDidCatch(),而是抛出未处理的异常。
dataService.getNetworkSummary().then(function (result) {
debugger
self.networks = result;
self.setState({
items: result.slice(0, 20),
total: result.length,
skip: 0,
pageSize: 20,
pageable: {
buttonCount: 5,
info: true,
type: 'numeric',
pageSizes: [20, 40, 60, 80, 100],
previousNext: true
},
sortable:{
allowUnsort: true,
mode: 'single',
sortDir:'Asc'
}
})
}).catch(err =>
{
throw new Error("This is my error");
});
有什么方法可以以正确的方式做到这一点。
【问题讨论】:
标签: reactjs react-native error-handling react-redux react-router