Run updates in React
Use useRun to show a run's status, progress, and final output as they change.
useRun follows one run. Your component re-renders when the run's status, metadata, or output changes.
'use client'
import { useRun } from '@runlane/react'
import type { writeAnswer } from './tasks/write-answer'
export function RunStatusBadge({ runId }: { runId: string }) {
const { run, error } = useRun<typeof writeAnswer>(runId)
if (error) return <p>Could not load the run.</p>
if (!run) return <p>Loading…</p>
return <p>Status: {run.status}</p>
}runisundefineduntil the first snapshot arrives.typeof writeAnswertypesrun.outputfrom the task.import typekeeps backend code out of the browser.- The component needs a
RunlaneProviderabove it.
See the run object for every field on run.
Show progress
Save progress from the task with context.updateMeta():
import { task } from '@runlane/core'
import * as z from 'zod'
import { resize } from '../images'
export const resizeImages = task({
id: 'images.resize',
schema: z.object({ urls: z.array(z.string()) }),
async run({ urls }, context) {
for (const [index, url] of urls.entries()) {
await resize(url)
await context.updateMeta({ done: index + 1, total: urls.length })
}
},
})Then read it from run.meta:
const { run } = useRun<typeof resizeImages>(runId)
const done = run?.meta?.done
const total = run?.meta?.total
if (typeof done !== 'number' || typeof total !== 'number') return <p>Starting…</p>
return (
<progress value={done} max={total}>
{done} of {total}
</progress>
)Metadata is typed as JSON, so check each value's type before you use it. To skip the checks, declare the metadata type.
Keep metadata small. Each update is saved to storage. For a long stream of text or logs, use a stream instead.
Show the result
run.output holds the value the task returned. It is set once the run succeeds.
const { run } = useRun<typeof writeAnswer>(runId)
if (run?.status === 'succeeded') return <p>Done: {run.output?.parts} parts</p>
if (run?.status === 'failed') return <p>Failed: {run.failure?.message}</p>Compare status with plain strings. TypeScript checks them, so a typo such as 'finished' is an error.
output and failure arrive only if the grant includes the Output permission. A failed run is data, not an error, so error stays undefined.
Run code when the run finishes
onComplete runs once, when the run reaches a final status or the reader fails:
const { run } = useRun<typeof writeAnswer>(runId, {
onComplete(run, error) {
if (error) toast.error('Lost track of the run.')
else if (run?.status === 'succeeded') router.refresh()
},
})- On a final status,
errorisundefinedandrunholds the final data. - On a read failure,
erroris set.runholds the last data, orundefinedif no snapshot arrived. - It does not run after
stop(), after unmount, or when the run ID changes.
Show the connection
isConnected is true only while a connection is open. It is false before the first connection, while the client reconnects, and after the run ends. Use it for a simple "live" dot.
To show a "reconnecting" note, use onConnectionChange. It receives each state: connecting, open, reconnecting (with the attempt number and delay), and closed.
import { useState } from 'react'
import { useRun } from '@runlane/react'
const [reconnecting, setReconnecting] = useState(false)
const { run } = useRun<typeof writeAnswer>(runId, {
onConnectionChange(change) {
if (change.state === 'reconnecting') setReconnecting(true)
if (change.state === 'open' || change.state === 'closed') setReconnecting(false)
},
})
return (
<p>
{run?.status ?? 'Loading'} {reconnecting && '(reconnecting…)'}
</p>
)Wait for a run ID
Pass undefined until you have an ID. The hook opens nothing until then.
const [runId, setRunId] = useState<string>()
const { run } = useRun<typeof writeAnswer>(runId)You can also pass enabled: false to stop reading while you keep the ID. This closes the reader and clears its data. Setting it back to true opens a new reader.
Show a list of runs
Give each row its own useRun. All rows share the provider's client.
function RunRow({ runId }: { runId: string }) {
const { run, error } = useRun<typeof writeAnswer>(runId)
return <li>{error ? 'Unavailable' : (run?.status ?? 'Loading')}</li>
}
export function RunList({ runIds }: { runIds: readonly string[] }) {
return (
<ul>
{runIds.map((runId) => (
<RunRow key={runId} runId={runId} />
))}
</ul>
)
}Each row opens its own connection. Serve your app over HTTP/2 when a page follows many runs. See troubleshooting.
Options
useRun(runId, options?) takes the shared options plus:
| Option | Meaning |
|---|---|
onComplete(run, error?) | Called once on a final status or a read failure |
It returns { run, error, isConnected, stop }. See the React reference for exact types.