stream hooks / API reference

API Reference

useStream

A custom React hook for managing and interacting with a data stream.

Properties

useStream({ onBeforeStart, onStop }: UseStreamProps)
PropertyTypeRequiredDescription
onBeforeStartFunctionNoCallback function invoked before the stream starts.
onStopFunctionNoCallback function invoked when the stream stops.

Returns

PropertyTypeDescription
startStreamFunctionFunction to start the stream.
stopStreamFunctionFunction to stop the stream.

useJsonStream

Extends useStream to handle JSON streaming, with parsing based on a provided Zod schema.

Properties

useJsonStream({ onReceive, onEnd, schema, onBeforeStart, onStop, defaultData }: UseJsonStreamProps<T>)
PropertyTypeRequiredDescription
onReceiveFunctionNoCallback invoked with each piece of received data.
onEndFunctionNoCallback invoked when the stream ends.
schemaz.AnyZodObjectYesZod schema for validating and parsing the JSON data.
onBeforeStartFunctionNoCallback function invoked before the stream starts.
onStopFunctionNoCallback function invoked when the stream stops.
defaultDataObjectNoDefault data to use before any stream data arrives.

Returns

PropertyTypeDescription
startStreamFunctionFunction to start the stream.
stopStreamFunctionFunction to stop the stream.
dataObjectThe latest piece of parsed data from the stream.
loadingbooleanIndicates whether the stream is currently active.