Capítulo 56 de 80
streamedQuery wraps an AsyncIterable-returning function into a normal queryFn — the query goes pending until the first chunk arrives, then success (with an array of chunks-so-far), staying fetchStatus: 'fetching' until the stream ends — the building block behind chat/streaming-response UIs.
experimental_streamedQuery — still gathering feedback, name/shape may change.streamFn: (context: QueryFunctionContext) => Promise<AsyncIterable<TData>>, required — receives the normal QueryFunctionContext (key, signal, etc.), returns something iterable chunk-by-chunk.refetchMode (default 'reset'): controls what a refetch does to already-streamed data — 'reset' clears everything back to pending; 'append' adds new chunks onto existing data; 'replace' buffers the whole new stream and swaps it in atomically once it finishes.reducer: combines each incoming chunk into the accumulated TData — defaults to array-append when TData is an array; mandatory to supply your own if TData isn't array-shaped (e.g. accumulating into a single growing string).initialValue: the value shown before the first chunk arrives, and the fallback if the stream yields nothing — defaults to an empty array; required alongside a custom reducer.import { experimental_streamedQuery as streamedQuery } from '@tanstack/react-query'
const chatQuery = queryOptions({
queryKey: ['chat', conversationId],
queryFn: streamedQuery({ streamFn: fetchChatResponseInChunks }),
})
queryOptions-compatible queryFn.useQuery.reducer (and initialValue) the moment the accumulated shape isn't a plain array — the default reducer only handles array accumulation.QueryFunctionContext this wraps.useSuspenseQuery for a chat UI that suspends until the first chunk.