Files
portfolio/components/ErrorBoundary.tsx

43 lines
1.2 KiB
TypeScript
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use client"; // <--- Diese Zeile ist PFLICHT für Error Boundaries!
import React from "react";
// Wir nutzen "export default", damit der Import ohne Klammern funktioniert
export default class ErrorBoundary extends React.Component<
{ children: React.ReactNode },
{ hasError: boolean }
> {
constructor(props: { children: React.ReactNode }) {
super(props);
this.state = { hasError: false };
}
static getDerivedStateFromError(_error: unknown) {
return { hasError: true };
}
componentDidCatch(error: unknown, errorInfo: React.ErrorInfo) {
console.error("ErrorBoundary caught an error:", error, errorInfo);
}
render() {
if (this.state.hasError) {
// Still render children to prevent white screen - just log the error
if (process.env.NODE_ENV === 'development') {
return (
<div>
<div className="p-2 m-2 bg-yellow-50 border border-yellow-200 rounded text-yellow-800 text-xs">
Error boundary triggered - rendering children anyway
</div>
{this.props.children}
</div>
);
}
// In production, just render children silently
return this.props.children;
}
return this.props.children;
}
}