Installation
Install the@trigger.dev/react-hooks package in your project:
Authentication
Before you can use the hooks, you need to provide a public access token to theTriggerAuthContext provider. Learn more about authentication in the frontend guide.
baseURL to the TriggerAuthContext provider.
Next.js and client components
If you are using Next.js with the App Router, you have to make sure the component that uses theTriggerAuthContext is a client component. So for example, the following code will not work:
app/page.tsx
Page is a server component and the TriggerAuthContext.Provider uses client-only react code. To fix this, wrap the TriggerAuthContext.Provider in a client component:
components/TriggerProvider.tsx
Passing the token to the frontend
Techniques for passing the token to the frontend vary depending on your setup. Here are a few ways to do it for different setups:Next.js App Router
If you are using Next.js with the App Router and you are triggering a task from a server action, you can use cookies to store and pass the token to the frontend.actions/trigger.ts
/runs/[id].tsx page, you can read the token from the cookie and pass it to the TriggerProvider.
pages/runs/[id].tsx
actions/trigger.ts
/runs/[id].tsx page:
pages/runs/[id].tsx
Usage
SWR vs Realtime hooks
We offer two “styles” of hooks: SWR and Realtime. The SWR hooks use the swr library to fetch data once and cache it. The Realtime hooks use Trigger.dev realtime to subscribe to updates in real-time.It can be a little confusing which one to use because swr can also be
configured to poll for updates. But because of rate-limits and the way the Trigger.dev API works,
we recommend using the Realtime hooks for most use-cases.
useRealtime* are Realtime hooks, and all hooks named use* are SWR hooks.
Common SWR hook options
You can pass the following options to the all SWR hooks:boolean
Revalidate the data when the window regains focus.
boolean
Revalidate the data when the browser regains a network connection.
number
Poll for updates at the specified interval (in milliseconds). Polling is not recommended for most
use-cases. Use the Realtime hooks instead.
Common SWR hook return values
Error
An error object if an error occurred while fetching the data.
boolean
A boolean indicating if the data is currently being fetched.
boolean
A boolean indicating if the data is currently being revalidated.
boolean
A boolean indicating if an error occurred while fetching the data.
useRun
TheuseRun hook allows you to fetch a run by its ID.
run object returned is the same as the run object returned by the Trigger.dev API. To correctly type the run’s payload and output, you can provide the type of your task to the useRun hook:
useRealtimeRun
TheuseRealtimeRun hook allows you to subscribe to a run by its ID.
useRealtimeRun hook:
useRealtimeRunsWithTag
TheuseRealtimeRunsWithTag hook allows you to subscribe to multiple runs with a specific tag.
useRealtimeRunsWithTag hook:
useRealtimeRunsWithTag could return multiple different types of tasks, you can pass a union of all the task types to the hook:
useRealtimeBatch
TheuseRealtimeBatch hook allows you to subscribe to a batch of runs by its the batch ID.

