fix: Remove the onRenderError prop and add an ErrorBoundary component

This commit is contained in:
Johannes Schill
2019-01-08 13:32:08 +01:00
parent bcb94cc0ec
commit f428db282c
6 changed files with 71 additions and 19 deletions

View File

@@ -0,0 +1,47 @@
import React, { Component } from 'react';
interface ErrorInfo {
componentStack: string;
}
interface RenderProps {
error: Error;
errorInfo: ErrorInfo;
}
interface Props {
children: (r: RenderProps) => JSX.Element;
}
interface State {
error: Error;
errorInfo: ErrorInfo;
}
class ErrorBoundary extends Component<Props, State> {
constructor(props) {
super(props);
this.state = { error: null, errorInfo: null };
}
componentDidCatch(error: Error, errorInfo: ErrorInfo) {
this.setState({
error: error,
errorInfo: errorInfo
});
}
render() {
const { error, errorInfo } = this.state;
return (
<>
{this.props.children({
error,
errorInfo,
})}
</>
);
}
}
export default ErrorBoundary;