Fallback component for Error boundry

This commit is contained in:
Dustin Brett
2022-06-29 20:18:12 -07:00
parent 7115f756a9
commit b1c8a3ad1f
4 changed files with 25 additions and 7 deletions

View File

@@ -2,7 +2,7 @@ import type React from "react";
import { Component } from "react";
type ErrorBoundaryProps = {
reloadOnError?: boolean;
FallbackRender?: React.ReactNode;
};
type ErrorBoundaryState = {
@@ -28,15 +28,14 @@ export class ErrorBoundary extends Component<
public render(): React.ReactNode {
const {
props: { children, reloadOnError },
props: { children, FallbackRender },
state: { hasError },
} = this;
if (hasError && reloadOnError) {
if (hasError && !FallbackRender) {
window.location.reload();
}
// eslint-disable-next-line unicorn/no-null
return hasError ? null : children;
return hasError ? FallbackRender : children;
}
}

View File

@@ -0,0 +1,18 @@
import styled from "styled-components";
const StyledComponentError = styled.div`
display: flex;
font-size: 22px;
height: 100%;
place-content: center;
place-items: center;
width: 100%;
`;
const ERROR_MESSAGE = "Error occured while loading component.";
const ComponentError: FC = () => (
<StyledComponentError>{ERROR_MESSAGE}</StyledComponentError>
);
export default ComponentError;

View File

@@ -1,4 +1,5 @@
import { ErrorBoundary } from "components/pages/ErrorBoundary";
import ComponentError from "components/system/Apps/ComponentError";
import dynamic from "next/dynamic";
const Window = dynamic(() => import("components/system/Window"));
@@ -19,7 +20,7 @@ const RenderComponent: FC<RenderComponentProps> = ({
id,
}) => {
const SafeComponent = (
<ErrorBoundary>
<ErrorBoundary FallbackRender={<ComponentError />}>
<Component id={id} />
</ErrorBoundary>
);

View File

@@ -14,7 +14,7 @@ const Index = (): React.ReactElement => {
useLockTitle();
return (
<ErrorBoundary reloadOnError>
<ErrorBoundary>
<Desktop>
<Taskbar />
<AppsLoader />