UNPKG

@janiscommerce/ui-native

Version:
38 lines (37 loc) 1.08 kB
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 />; } return this.props.children; } } export default ErrorBoundary;