๐ Reference
# yarn
yarn add react-query
# npm
npm i react-query
# ์๋ฌ๊ฐ ๋ฐ์ํ ๊ฒฝ์ฐ
npm i react-query --legacy-peer-deps
_app.tsx
์์ 1
// _app.tsx
import "../styles/globals.css";
import type { AppProps } from "next/app";
import { useState } from "react";
import { QueryClient, QueryClientProvider } from "react-query";
import { ReactQueryDevtools } from "react-query/devtools";
declare module "react-query/types/react/QueryClientProvider" {
interface QueryClientProviderProps {
children?: React.ReactNode;
}
}
function MyApp({ Component, pageProps }: AppProps) {
const [queryClient] = useState(() => new QueryClient());
return (
<>
<QueryClientProvider client={queryClient}>
<Component {...pageProps} />
<ReactQueryDevtools initialIsOpen={false} position="bottom-right" />
</QueryClientProvider>
</>
);
}
export default MyApp;
QueryClient()
const [queryClient] = useState(() => new QueryClient());
QueryClient
์ธ์คํด์ค๋ฅผ ์์ฑํ ํ QueryClientProvider
๋ฅผ ํตํด ์ปดํฌ๋ํธ๊ฐ QueryClient
์ธ์คํด์ค์ ์ ๊ทผํ ์ ์๋๋ก App
์ปดํฌ๋ํธ ์ต์๋จ์ ์ถ๊ฐํ๋ค.QueryClient
๋ฅผ ์ด์ฉํ์ฌ QueryClient
์ธ์คํด์ค๋ฅผ ๋ง๋ค์ด ์ค๋ค.QueryClientProvider
์ props
๋ก ์ ๋ฌํด์ค๋ค.ReactQueryDevtools
<ReactQueryDevtools initialIsOpen={false} position="bottom-right" />
ReactQueryDevtools
๋ ๊ฐ๋ฐ ๋๊ตฌ๋ก ๊ฐ๋ฐ ํ๊ฒฝ์ด development(NODE_ENV=development)
์ผ ๋๋ง ๋ํ๋๋ค.QueryClientProviderProps
declare module "react-query/types/react/QueryClientProvider" {
interface QueryClientProviderProps {
children?: React.ReactNode;
}
}
Delcare Module ~ interface
์ฝ๋๋ React๊ฐ 18๋ฒ์ ์ผ๋ก ์
๋ฐ์ดํธ ๋๋ฉด์ ๋ํ๋๋ ํ์
์ด์๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด ์์ฑํ ์ฝ๋์ด๋ค.QueryClientProvider
return (
<>
<QueryClientProvider client={queryClient}>
<Component {...pageProps} />
<ReactQueryDevtools initialIsOpen={false} position="bottom-right" />
</QueryClientProvider>
</>
);
App.js
ํน์ App.tsx
์์ QueryClientProvider
๋ก ๊ฐ์ธ์ฃผ๋ฉด ๋๋ค.์์ 2
// _app.tsx
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
// NEVER DO THIS:
// const queryClient = new QueryClient()
//
// Creating the queryClient at the file root level makes the cache shared
// between all requests and means _all_ data gets passed to _all_ users.
// Besides being bad for performance, this also leaks any sensitive data.
export default function MyApp({ Component, pageProps }) {
// Instead do this, which ensures each request has its own cache:
const [queryClient] = React.useState(
() =>
new QueryClient({
defaultOptions: {
queries: {
// With SSR, we usually want to set some default staleTime
// above 0 to avoid refetching immediately on the client
staleTime: 60 * 1000,
},
},
}),
)
return (
<QueryClientProvider client={queryClient}>
<Component {...pageProps} />
</QueryClientProvider>
)
}