Skip to content

Commit

Permalink
feat(error-boundary): create errorBundary and refactor withSuspence (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrykKuniczak authored Oct 15, 2023
1 parent e067e8c commit 6a8adb9
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/pages/newtab/Newtab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "@pages/newtab/Newtab.scss";
import useStorage from "@src/shared/hooks/useStorage";
import exampleThemeStorage from "@src/shared/storages/exampleThemeStorage";
import withSuspense from "@src/shared/hoc/withSuspense";
import withErrorBoundary from "@src/shared/hoc/withErrorBoundary";

const Newtab = () => {
const theme = useStorage(exampleThemeStorage);
Expand Down Expand Up @@ -41,4 +42,7 @@ const Newtab = () => {
);
};

export default withSuspense(Newtab);
export default withErrorBoundary(
withSuspense(Newtab, <div> Loading ... </div>),
<div> Error Occur </div>
);
6 changes: 5 additions & 1 deletion src/pages/popup/Popup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import "@pages/popup/Popup.css";
import useStorage from "@src/shared/hooks/useStorage";
import exampleThemeStorage from "@src/shared/storages/exampleThemeStorage";
import withSuspense from "@src/shared/hoc/withSuspense";
import withErrorBoundary from "@src/shared/hoc/withErrorBoundary";

const Popup = () => {
const theme = useStorage(exampleThemeStorage);
Expand Down Expand Up @@ -36,4 +37,7 @@ const Popup = () => {
);
};

export default withSuspense(Popup);
export default withErrorBoundary(
withSuspense(Popup, <div> Loading ... </div>),
<div> Error Occur </div>
);
42 changes: 42 additions & 0 deletions src/shared/hoc/withErrorBoundary.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Component, ComponentType, ReactElement } from "react";

class ErrorBoundary extends Component<
{
children: ReactElement;
fallback: ReactElement;
},
{
hasError: boolean;
}
> {
state = { hasError: false };

static getDerivedStateFromError() {
return { hasError: true };
}

componentDidCatch(error, errorInfo) {
console.error(error, errorInfo);
}

render() {
if (this.state.hasError) {
return this.props.fallback;
}

return this.props.children;
}
}

export default function withErrorBoundary<T extends Record<string, unknown>>(
Component: ComponentType<T>,
ErrorComponent: ReactElement
) {
return function WithErrorBoundary(props: T) {
return (
<ErrorBoundary fallback={ErrorComponent}>
<Component {...props} />
</ErrorBoundary>
);
};
}
4 changes: 2 additions & 2 deletions src/shared/hoc/withSuspense.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ComponentType, ReactNode, Suspense } from "react";
import { ComponentType, ReactElement, Suspense } from "react";

export default function withSuspense<T extends Record<string, unknown>>(
Component: ComponentType<T>,
SuspenseComponent: ReactNode = null
SuspenseComponent: ReactElement
) {
return function WithSuspense(props: T) {
return (
Expand Down

0 comments on commit 6a8adb9

Please sign in to comment.