useStream
Call Signature
Section titled “Call Signature”function useStream<TYield, TState, TReturn>(options): UseStreamReducerResult<TYield, TState>;Defined in: stream-renderer.tsx:422
Hook for consuming streams using TanStack Query’s streamedQuery.
Type Parameters
Section titled “Type Parameters”| Type Parameter | Default type |
|---|---|
TYield |
- |
TState |
- |
TReturn |
void |
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
options |
UseStreamReducerOptions<TYield, TState, TReturn> |
Returns
Section titled “Returns”UseStreamReducerResult<TYield, TState>
Example
Section titled “Example”// Default: accumulate all itemsconst { items, isStreaming, count } = useStream({ source: fetchItems, queryKey: ['my-stream'],});
// Latest only: keep just the most recent itemconst { items, latest } = useStream({ source: sseEvents, mode: "latest",});
// Sliding window: keep last N itemsconst { items } = useStream({ source: logStream, maxItems: 100,});
// Custom reducer: full control over stateconst { state } = useStream({ source: events, reducer: (state, event) => ({ ...state, [event.id]: event }), initialState: {},});Call Signature
Section titled “Call Signature”function useStream<TYield, TReturn>(options): UseStreamResult<TYield, TReturn>;Defined in: stream-renderer.tsx:427
Hook for consuming streams using TanStack Query’s streamedQuery.
Type Parameters
Section titled “Type Parameters”| Type Parameter | Default type |
|---|---|
TYield |
- |
TReturn |
void |
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
options |
| UseStreamAccumulateOptions<TYield, TReturn> | UseStreamLatestOptions<TYield, TReturn> | UseStreamMaxItemsOptions<TYield, TReturn> |
Returns
Section titled “Returns”UseStreamResult<TYield, TReturn>
Example
Section titled “Example”// Default: accumulate all itemsconst { items, isStreaming, count } = useStream({ source: fetchItems, queryKey: ['my-stream'],});
// Latest only: keep just the most recent itemconst { items, latest } = useStream({ source: sseEvents, mode: "latest",});
// Sliding window: keep last N itemsconst { items } = useStream({ source: logStream, maxItems: 100,});
// Custom reducer: full control over stateconst { state } = useStream({ source: events, reducer: (state, event) => ({ ...state, [event.id]: event }), initialState: {},});