Skip to content

Commit

Permalink
fix: AsyncBoundary
Browse files Browse the repository at this point in the history
  • Loading branch information
tooooo1 committed Aug 31, 2024
1 parent e230301 commit 3bc4084
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
27 changes: 27 additions & 0 deletions src/components/AsyncBoundary.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { PropsWithChildren, Suspense, type ComponentProps } from 'react';
import { ErrorBoundary, type FallbackProps } from 'react-error-boundary';

type ErrorBoundaryProps = ComponentProps<typeof ErrorBoundary>;

interface AsyncBoundaryProps extends Omit<ErrorBoundaryProps, 'fallbackRender'> {
pendingFallback: ComponentProps<typeof Suspense>['fallback'];
rejectedFallback: ErrorBoundaryProps['fallbackRender'];
}

const Substitute = ({ error }: FallbackProps) => {
return <>{error.message}</>;
};

const AsyncBoundary = ({
pendingFallback,
rejectedFallback,
children,
}: PropsWithChildren<AsyncBoundaryProps>) => {
return (
<ErrorBoundary fallbackRender={rejectedFallback || Substitute}>
<Suspense fallback={pendingFallback}>{children}</Suspense>
</ErrorBoundary>
);
};

export default AsyncBoundary;
16 changes: 10 additions & 6 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { QueryClient, QueryClientProvider } from 'react-query';
import { ReactQueryDevtools } from 'react-query/devtools';
import { RecoilRoot } from 'recoil';
import App from './App';
import AsyncBoundary from 'components/AsyncBoundary';
import { BadGateway } from 'pages';
initialize('G-KG7KQ8K3GP');

export const queryClient = new QueryClient({
Expand All @@ -27,11 +29,13 @@ axios.defaults.withCredentials = true;

ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<QueryClientProvider client={queryClient}>
<RecoilRoot>
<App />
<ReactQueryDevtools />
</RecoilRoot>
</QueryClientProvider>
<AsyncBoundary pendingFallback={<></>} rejectedFallback={() => <BadGateway />}>
<QueryClientProvider client={queryClient}>
<RecoilRoot>
<App />
<ReactQueryDevtools />
</RecoilRoot>
</QueryClientProvider>
</AsyncBoundary>
</React.StrictMode>
);

0 comments on commit 3bc4084

Please sign in to comment.