Skip to content

Instantly share code, notes, and snippets.

@codigoconjuan
Last active November 16, 2024 20:31
Show Gist options
  • Save codigoconjuan/da940c37ee21facd0dd6b8de30400890 to your computer and use it in GitHub Desktop.
Save codigoconjuan/da940c37ee21facd0dd6b8de30400890 to your computer and use it in GitHub Desktop.
Configurar React-Query con Next 15
'use client'
import {
isServer,
QueryClient,
QueryClientProvider,
} from '@tanstack/react-query'
function makeQueryClient() {
return new QueryClient({
defaultOptions: {
queries: {
// Mayor a 0 para evitar volver a obtener los datos inmediatamente en el cliente.
staleTime: 60 * 1000,
},
},
})
}
let browserQueryClient: QueryClient | undefined = undefined
function getQueryClient() {
if (isServer) {
// Servidor: siempre generar un Query Client
return makeQueryClient()
} else {
// Cliente: Solo genera el QueryClient si no tenemos uno
if (!browserQueryClient) browserQueryClient = makeQueryClient()
return browserQueryClient
}
}
export default function Providers({ children }: {children: React.ReactNode}) {
const queryClient = getQueryClient()
return (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment