snapshot of https://github.com/TanStack/query.git (main, 2026-09-16; blobs > 131072B omitted)
This commit is contained in:
BIN
examples/react/nextjs-app-prefetching/app/favicon.ico
Normal file
BIN
examples/react/nextjs-app-prefetching/app/favicon.ico
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 25 KiB |
@@ -0,0 +1,37 @@
|
||||
import {
|
||||
QueryClient,
|
||||
defaultShouldDehydrateQuery,
|
||||
environmentManager,
|
||||
} from '@tanstack/react-query'
|
||||
|
||||
function makeQueryClient() {
|
||||
return new QueryClient({
|
||||
defaultOptions: {
|
||||
queries: {
|
||||
staleTime: 60 * 1000,
|
||||
},
|
||||
dehydrate: {
|
||||
// include pending queries in dehydration
|
||||
shouldDehydrateQuery: (query) =>
|
||||
defaultShouldDehydrateQuery(query) ||
|
||||
query.state.status === 'pending',
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
let browserQueryClient: QueryClient | undefined = undefined
|
||||
|
||||
export function getQueryClient() {
|
||||
if (environmentManager.isServer()) {
|
||||
// Server: always make a new query client
|
||||
return makeQueryClient()
|
||||
} else {
|
||||
// Browser: make a new query client if we don't already have one
|
||||
// This is very important, so we don't re-make a new client if React
|
||||
// suspends during the initial render. This may not be needed if we
|
||||
// have a suspense boundary BELOW the creation of the query client
|
||||
if (!browserQueryClient) browserQueryClient = makeQueryClient()
|
||||
return browserQueryClient
|
||||
}
|
||||
}
|
||||
22
examples/react/nextjs-app-prefetching/app/layout.tsx
Normal file
22
examples/react/nextjs-app-prefetching/app/layout.tsx
Normal file
@@ -0,0 +1,22 @@
|
||||
import Providers from './providers'
|
||||
import type React from 'react'
|
||||
import type { Metadata } from 'next'
|
||||
|
||||
export const metadata: Metadata = {
|
||||
title: 'Create Next App',
|
||||
description: 'Generated by create next app',
|
||||
}
|
||||
|
||||
export default function RootLayout({
|
||||
children,
|
||||
}: {
|
||||
children: React.ReactNode
|
||||
}) {
|
||||
return (
|
||||
<html lang="en">
|
||||
<body>
|
||||
<Providers>{children}</Providers>
|
||||
</body>
|
||||
</html>
|
||||
)
|
||||
}
|
||||
20
examples/react/nextjs-app-prefetching/app/page.tsx
Normal file
20
examples/react/nextjs-app-prefetching/app/page.tsx
Normal file
@@ -0,0 +1,20 @@
|
||||
import React from 'react'
|
||||
import { HydrationBoundary, dehydrate, noop } from '@tanstack/react-query'
|
||||
import { pokemonOptions } from '@/app/pokemon'
|
||||
import { getQueryClient } from '@/app/get-query-client'
|
||||
import { PokemonInfo } from './pokemon-info'
|
||||
|
||||
export default function Home() {
|
||||
const queryClient = getQueryClient()
|
||||
|
||||
void queryClient.query(pokemonOptions).catch(noop)
|
||||
|
||||
return (
|
||||
<main>
|
||||
<h1>Pokemon Info</h1>
|
||||
<HydrationBoundary state={dehydrate(queryClient)}>
|
||||
<PokemonInfo />
|
||||
</HydrationBoundary>
|
||||
</main>
|
||||
)
|
||||
}
|
||||
18
examples/react/nextjs-app-prefetching/app/pokemon-info.tsx
Normal file
18
examples/react/nextjs-app-prefetching/app/pokemon-info.tsx
Normal file
@@ -0,0 +1,18 @@
|
||||
'use client'
|
||||
|
||||
import React from 'react'
|
||||
import { useSuspenseQuery } from '@tanstack/react-query'
|
||||
import { pokemonOptions } from '@/app/pokemon'
|
||||
|
||||
export function PokemonInfo() {
|
||||
const { data } = useSuspenseQuery(pokemonOptions)
|
||||
|
||||
return (
|
||||
<div>
|
||||
<figure>
|
||||
<img src={data.sprites.front_shiny} height={200} alt={data.name} />
|
||||
<h2>I'm {data.name}</h2>
|
||||
</figure>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
10
examples/react/nextjs-app-prefetching/app/pokemon.ts
Normal file
10
examples/react/nextjs-app-prefetching/app/pokemon.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { queryOptions } from '@tanstack/react-query'
|
||||
|
||||
export const pokemonOptions = queryOptions({
|
||||
queryKey: ['pokemon'],
|
||||
queryFn: async () => {
|
||||
const response = await fetch('https://pokeapi.co/api/v2/pokemon/25')
|
||||
|
||||
return response.json()
|
||||
},
|
||||
})
|
||||
16
examples/react/nextjs-app-prefetching/app/providers.tsx
Normal file
16
examples/react/nextjs-app-prefetching/app/providers.tsx
Normal file
@@ -0,0 +1,16 @@
|
||||
'use client'
|
||||
import { QueryClientProvider } from '@tanstack/react-query'
|
||||
import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
|
||||
import { getQueryClient } from '@/app/get-query-client'
|
||||
import type * as React from 'react'
|
||||
|
||||
export default function Providers({ children }: { children: React.ReactNode }) {
|
||||
const queryClient = getQueryClient()
|
||||
|
||||
return (
|
||||
<QueryClientProvider client={queryClient}>
|
||||
{children}
|
||||
<ReactQueryDevtools />
|
||||
</QueryClientProvider>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user