React hooks
Install the React package, add the provider, and pick the right hook.
@runlane/react gives React components live access to runs. It has one provider and two hooks.
Install
npm install @runlane/react @runlane/realtimeThe hooks need React 19. You also need a mounted realtime handler. See set up realtime.
Add the provider
Wrap the part of your app that shows runs in RunlaneProvider. Set baseUrl to the path where you mounted the handler.
'use client'
import type { ReactNode } from 'react'
import { RunlaneProvider } from '@runlane/react'
export function ProgressProvider({ children }: { children: ReactNode }) {
return <RunlaneProvider baseUrl="/api/progress">{children}</RunlaneProvider>
}Render <ProgressProvider> around the pages that show runs, for example in a layout. Keep it in its own 'use client' file. In Next.js, a server layout cannot pass functions such as refreshAccessToken to a client component.
That is all you need for session cookies. For tokens, also pass refreshAccessToken, as shown in refresh expired tokens.
| Prop | Meaning |
|---|---|
baseUrl | Required. The handler's mount path, such as /api/progress, or a full URL on another origin. |
refreshAccessToken | Optional. Returns a fresh token for a run. See refresh expired tokens. |
fetch | Optional. A custom fetch function. |
telemetry, onTelemetryError | Optional. Record client metrics. See telemetry. |
The provider creates one shared client. Inline functions are fine: changing refreshAccessToken does not reopen readers.
A hook used outside a provider throws configuration_invalid during render. To skip the provider, pass a client with the client option instead.
Pick a hook
| Hook | Use it to show | Guide |
|---|---|---|
useRun | Status, progress from metadata, and the final output | Run updates in React |
useRunStream | Text or data the task streams while it runs | Streams in React |
Each hook opens its own connection. A component that uses both opens two.
Shared options
Both hooks take these options:
| Option | Meaning |
|---|---|
accessToken | Token for the first connection. Leave it out when you use cookies. |
enabled | Set to false to open nothing. Defaults to true. |
throttle | How long to group updates before a re-render, as a duration string such as '100ms'. Defaults to contractDefaults.realtime.renderThrottle. |
onConnectionChange | Called with each connection change: connecting, open, reconnecting, or closed. |
client | A client from createRunlaneClient(). Use it instead of the provider. |
Both hooks also return:
| Field | Meaning |
|---|---|
error | A RunlaneError if reading failed, otherwise undefined |
isConnected | true while the connection is open |
stop() | Closes the connection and keeps the last data |
When a hook reconnects
A hook opens a new reader, and clears its data, only when these change:
- the run ID,
enabled, orclient; - for
useRunStream, the stream name,mode, ormaxParts; - the provider's
baseUrlorfetch.
Changing accessToken, throttle, or a callback keeps the reader open. Callbacks always use their newest version.
Unmounting a component closes its reader. Pass undefined as the run ID to open nothing until you have one.
Server rendering
During server rendering, the hooks return empty data with isConnected: false. They make no network requests. Readers start after React renders the component in the browser.
Stopping is not cancelling
stop() and unmounting only close the connection. The run keeps going on the worker. To cancel a run, call runlane.runs.cancel(runId) on your server.
Errors
error is always a RunlaneError. Check its code:
| Code | What happened |
|---|---|
access_denied | The reader is not allowed to read this run, or its token could not be refreshed |
run_not_found | The run does not exist, or it was pruned |
disconnected | The client gave up after too many failed reconnects |
validation_failed | The run ID or stream name is not valid |
wire_invalid | The response was not from the realtime handler. Check baseUrl. See troubleshooting. |
Show your own message to users. Do not show error.cause; it can hold internal details. See the reference for every code.
A failed run is not an error. It arrives as data with run.status set to failed.