Skip to content

Commit

Permalink
♻️ refactor: move ErrorFallback inside component and add compact prop
Browse files Browse the repository at this point in the history
Improves component encapsulation by moving ErrorFallback inside ErrorBoundary
and adds support for compact error display through optional prop parameter
  • Loading branch information
w3bdesign committed Dec 7, 2024
1 parent 678ddc8 commit 1fafe90
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/components/ErrorBoundary/ErrorBoundary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ import Fallback from "./Fallback.component";

interface ErrorBoundaryProps {
children: ReactNode;
}

// Define the fallback component outside of ErrorBoundary
function ErrorFallback(props: FallbackProps) {
return <Fallback {...props} compact={false} />;
compact?: boolean;
}

/**
Expand All @@ -19,13 +15,18 @@ function ErrorFallback(props: FallbackProps) {
*
* @param {Object} props - The component props
* @param {ReactNode} props.children - The child components to be wrapped by the ErrorBoundary
* @param {boolean} props.compact - Whether to show a compact error fallback (used in stories)
* @returns {JSX.Element} A React component that catches errors in its child components
*/
const ErrorBoundary: React.FC<ErrorBoundaryProps> = ({ children }) => {
const ErrorBoundary: React.FC<ErrorBoundaryProps> = ({ children, compact = false }) => {
const handleError = (error: Error, info: ErrorInfo) => {
console.error("Uventet feil i Matrix:", error, info);
};

const ErrorFallback = (props: FallbackProps) => {
return <Fallback {...props} compact={compact} />;
};

return (
<ReactErrorBoundary
FallbackComponent={ErrorFallback}
Expand Down

0 comments on commit 1fafe90

Please sign in to comment.