@janiscommerce/ui-native
Version:
components library for Janis app
38 lines (37 loc) • 1.24 kB
JavaScript
import React from 'react';
import ErrorFallback from '../ErrorBoundary/components/ErrorFallback';
class ErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = {
hasError: false,
error: null,
errorInfo: null,
};
}
static getDerivedStateFromError(error) {
return { hasError: true, error };
}
componentDidMount() {
if (this.props.onMount) {
this.props.onMount();
}
}
componentDidCatch(error, errorInfo) {
this.setState({ errorInfo });
if (this.props.onError) {
this.props.onError(error, errorInfo);
}
}
render() {
if (this.state.hasError) {
const { renderErrorComponent } = this.props;
if (typeof renderErrorComponent === 'function') {
return renderErrorComponent(this.state.error?.message || '');
}
return (<ErrorFallback isDebug={this.props.isDebug} errorMessage={this.props.errorMessage} error={this.state.error?.message} errorDetails={this.state.errorInfo?.componentStack}/>);
}
return this.props.children;
}
}
export default ErrorBoundary;