docs/reference/streamedQuery.md
streamedQuery is a helper function to create a query function that streams data from an AsyncIterable. Data will be an Array of all the chunks received. The query will be in a pending state until the first chunk of data is received, but will go to success after that. The query will stay in fetchStatus fetching until the stream ends.
To see streamedQuery in action, take a look at our chat example in the examples/react/chat directory on GitHub.
import { experimental_streamedQuery as streamedQuery } from '@tanstack/react-query'
const query = queryOptions({
queryKey: ['data'],
queryFn: streamedQuery({
streamFn: fetchDataInChunks,
}),
})
Note:
streamedQueryis currently marked asexperimentalbecause we want to gather feedback from the community. If you've tried out the API and have feedback for us, please provide it in this GitHub discussion.
Options
streamFn: (context: QueryFunctionContext) => Promise<AsyncIterable<TData>>
refetchMode?: 'append' | 'reset' | 'replace'
'reset''reset', the query will erase all data and go back into pending state.'append', data will be appended to existing data.'replace', all data will be written to the cache once the stream ends.reducer?: (accumulator: TData, chunk: TQueryFnData) => TData
TQueryFnData) into the final data shape (TData).TData is an array.TData is not an array, you must provide a custom reducer.initialValue?: TData = TQueryFnData
reducer is provided.