Created
May 26, 2023 14:52
-
-
Save pkellner/7c920493b709731b8fefc758b8d4efe2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use client"; | |
import React, { ReactNode } from "react"; | |
import { ErrorBoundary } from "react-error-boundary"; | |
export default function ErrorBoundaryFunctionalWrapper({ | |
children, | |
errorComponent, | |
}: { | |
children: ReactNode; | |
errorComponent?: ReactNode; | |
}) { | |
function Fallback({ error }: { error: Error }) { | |
return ( | |
<div role="alert"> | |
<p>Something went wrong:</p> | |
<pre style={{ color: "red" }}>{error.message}</pre> | |
</div> | |
); | |
} | |
if (!errorComponent) { | |
return ( | |
<> | |
{ | |
/* @ts-ignore */ | |
<ErrorBoundary FallbackComponent={Fallback}>{children}</ErrorBoundary> | |
} | |
</> | |
); | |
} | |
return ( | |
<> | |
{ | |
/* @ts-ignore */ | |
<ErrorBoundary FallbackComponent={errorComponent}>{children}</ErrorBoundary> | |
} | |
</> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment