Last active
November 16, 2024 20:31
-
-
Save codigoconjuan/da940c37ee21facd0dd6b8de30400890 to your computer and use it in GitHub Desktop.
Configurar React-Query con Next 15
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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