UNPKG

@winglet/react-utils

Version:

React utility library providing custom hooks, higher-order components (HOCs), and utility functions to enhance React application development with improved reusability and functionality

31 lines (28 loc) 783 B
import { jsx } from 'react/jsx-runtime'; import { Component } from 'react'; import { FallbackMessage } from './FallbackMessage.mjs'; const FALLBACK = jsx(FallbackMessage, {}); class ErrorBoundary extends Component { constructor(props) { super(props); this.state = { hasError: false, error: undefined, }; } static getDerivedStateFromError(error) { return { hasError: true, error, }; } componentDidCatch(error, errorInfo) { console.error('ErrorBoundary caught an error:', error, errorInfo); } render() { if (this.state.hasError) return this.props.fallback || FALLBACK; return this.props.children; } } export { ErrorBoundary };