Skip to content

useStream

useStream(options): UseStreamReturn

Defined in: react-hooks/src/use-stream.ts:79

useStream - React hook for generic text streaming.

Provides state management for streaming text from various sources including fetch responses, ReadableStreams, and async iterables.

UseStreamOptions = {}

UseStreamReturn

import { useStream } from 'ai.matey.react.hooks';
function StreamingComponent() {
const { text, isStreaming, startStream, stop } = useStream({
onComplete: (fullText) => console.log('Completed:', fullText),
});
const handleClick = async () => {
const response = await fetch('/api/stream');
await startStream(response);
};
return (
<div>
<button onClick={handleClick} disabled={isStreaming}>Start</button>
<button onClick={stop} disabled={!isStreaming}>Stop</button>
<pre>{text}</pre>
</div>
);
}