Skip to content

Instantly share code, notes, and snippets.

@pkellner
Created May 26, 2023 14:52
Show Gist options
  • Save pkellner/7c920493b709731b8fefc758b8d4efe2 to your computer and use it in GitHub Desktop.
Save pkellner/7c920493b709731b8fefc758b8d4efe2 to your computer and use it in GitHub Desktop.
"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