fern/03-reference/baml_client/react-nextjs/hook-output.mdx
The HookOutput type defines the return type for BAML React hooks.
return ( <div> {hook.error && <div>Error: {hook.error.message}</div>} <button onClick={() => hook.mutate("test")} disabled={hook.isLoading}> Submit </button> </div> ) }
```typescript title="Example Types"
// Streaming configuration
const streamingResult: HookOutput<'TestAws', { stream: true }> = {
data: "Any response",
finalData: "Final response",
streamData: "Streaming response...",
error: undefined,
isError: false,
isLoading: true,
isSuccess: false,
isStreaming: true,
isPending: false,
status: 'streaming',
mutate: async () => new ReadableStream(),
reset: () => void
}
// Non-streaming configuration
const nonStreamingResult: HookOutput<'TestAws', { stream: false }> = {
data: "Final response",
finalData: "Final response",
error: undefined,
isError: false,
isLoading: false,
isSuccess: true,
isPending: false,
status: 'success',
mutate: async () => "Final response",
reset: () => void
}
type HookOutput<FunctionName, Options extends { stream?: boolean } = { stream?: true }> = {
data?: Options['stream'] extends false ? FinalDataType<FunctionName> : FinalDataType<FunctionName> | StreamDataType<FunctionName>
finalData?: FinalDataType<FunctionName>
streamData?: Options['stream'] extends false ? never : StreamDataType<FunctionName>
error?: BamlErrors
isError: boolean
isLoading: boolean
isPending: boolean
isSuccess: boolean
isStreaming: Options['stream'] extends false ? never : boolean
status: HookStatus<Options>
mutate: (...args: Parameters<ServerAction>) => Options['stream'] extends false
? Promise<FinalDataType<FunctionName>>
: Promise<ReadableStream<Uint8Array>>
reset: () => void
}
type HookStatus<Options extends { stream?: boolean }> = Options['stream'] extends false
? 'idle' | 'pending' | 'success' | 'error'
: 'idle' | 'pending' | 'streaming' | 'success' | 'error'
<ParamField path="FunctionName" type="generic"
The name of the BAML function being called. Used to infer input and output types. </ParamField>
<ParamField path="Options" type="{ stream?: boolean }"
Configuration object that determines streaming behavior. Defaults to { stream?: true }.
</ParamField>
<ParamField path="data" type="FinalDataType<FunctionName> | StreamDataType<FunctionName> | undefined"
The current response data. For streaming hooks, this contains either the latest streaming response or the final response. For non-streaming hooks, this only contains the final response. </ParamField>
<ParamField path="finalData" type="FinalDataType<FunctionName> | undefined"
The final response data. Only set when the request completes successfully. </ParamField>
<ParamField path="streamData" type="StreamDataType<FunctionName> | undefined"
The latest streaming response. Only available when Options['stream'] is true.
</ParamField>
<ParamField path="error" type="BamlErrors | undefined"
Any error that occurred during the request. See Error Types. </ParamField>
<ParamField path="isError" type="boolean"
True if the request resulted in an error. </ParamField>
<ParamField path="isLoading" type="boolean"
True if the request is in progress (either pending or streaming). </ParamField>
<ParamField path="isPending" type="boolean"
True if the request is pending (not yet streaming or completed). </ParamField>
<ParamField path="isSuccess" type="boolean"
True if the request completed successfully. </ParamField>
<ParamField path="isStreaming" type="boolean"
True if the request is currently streaming data. Only available when Options['stream'] is true.
</ParamField>
<ParamField path="status" type="HookStatus<Options>"
The current status of the request. For streaming hooks: 'idle' | 'pending' | 'streaming' | 'success' | 'error'. For non-streaming hooks: 'idle' | 'pending' | 'success' | 'error'. </ParamField>
<ParamField path="mutate" type="(...args: Parameters<ServerAction>) => Promise<OutputType>"
Function to trigger the BAML action. Returns a ReadableStream for streaming hooks, or a Promise of the final response for non-streaming hooks. </ParamField>
<ParamField path="reset" type="() => void"
Function to reset the hook state back to its initial values. </ParamField>