Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

react-query suspense를 활용한 로딩과 errorboundary 설정 #85

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 3 additions & 18 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,40 +1,25 @@
"use client";

import React, { useEffect, useState } from "react";
import React from "react";
import { ToastContainer } from "react-toastify";
import { Inter } from "next/font/google";
import { RecoilRoot } from "recoil";
import "../styles/globals.css";
import ReactQueryProvider from "@/src/queries/ReactQueryProvider";
import BottomNavigationBar from "@/src/components/bottomNavigationBar";
import DefaultContainer from "../components/defaultContainer";
import LoadingScreen from "../components/loading";

const inter = Inter({ subsets: ["latin"] });

export default function RootLayout({ children }: { children: React.ReactNode }) {
const [isLoading, setIsLoading] = useState(true);

useEffect(() => {
// 데이터를 로딩하는 비동기 작업 예시
setTimeout(() => {
setIsLoading(false); // 데이터 로딩이 끝나면 isLoading 값을 false로 변경
}, 2000); // 2초 후에 isLoading을 false로 변경
}, []);
return (
<RecoilRoot>
<html lang="en">
<body className={inter.className}>
<ReactQueryProvider>
<DefaultContainer>
{isLoading ? (
<LoadingScreen />
) : (
<>
{children}
<BottomNavigationBar />
</>
)}
{children}
<BottomNavigationBar />
</DefaultContainer>
<ToastContainer position="bottom-center" hideProgressBar />
</ReactQueryProvider>
Expand Down
21 changes: 14 additions & 7 deletions src/queries/ReactQueryProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"use client";

import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { PropsWithChildren, useState } from "react";
import { QueryClient, QueryClientProvider, QueryErrorResetBoundary } from "@tanstack/react-query";
import React, { PropsWithChildren, Suspense, useState } from "react";
import { ReactQueryDevtools } from "@tanstack/react-query-devtools";
import LoadingScreen from "../components/loading";

export default function ReactQueryProvider({ children }: PropsWithChildren) {
const [queryClient] = useState(
Expand All @@ -11,16 +12,22 @@ export default function ReactQueryProvider({ children }: PropsWithChildren) {
// react-query 전역 설정
queries: {
refetchOnWindowFocus: false,
retry: false
retry: false,
suspense: true,
useErrorBoundary: true
}
}
})
);

return (
<QueryClientProvider client={queryClient}>
{children}
<ReactQueryDevtools initialIsOpen={false} />
</QueryClientProvider>
<QueryErrorResetBoundary>
<Suspense fallback={<LoadingScreen />}>
<QueryClientProvider client={queryClient}>
{children}
<ReactQueryDevtools initialIsOpen={false} />
</QueryClientProvider>
</Suspense>
</QueryErrorResetBoundary>
);
}