React hooks
Using the Trigger.dev v3 API from your React application.
Our react hooks package provides a set of hooks that make it easy to interact with the Trigger.dev API from your React application, using our frontend API. You can use these hooks to fetch runs, batches, and subscribe to real-time updates.
Installation
Install the @trigger.dev/react-hooks
package in your project:
Authentication
All hooks accept an optional last argument options
that accepts an accessToken
param, which should be a valid Public Access Token. Learn more about generating tokens in the frontend guide.
Alternatively, you can use our TriggerAuthContext
provider
Now children components can use the hooks to interact with the Trigger.dev API. If you are self-hosting Trigger.dev, you can provide the 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 the TriggerAuthContext
is a client component. So for example, the following code will not work:
That’s because 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:
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.
Then in the /runs/[id].tsx
page, you can read the token from the cookie and pass it to the TriggerProvider
.
Instead of a cookie, you could also use a query parameter to pass the token to the frontend:
And then in the /runs/[id].tsx
page:
Another alternative would be to use a server-side rendered page to fetch the token and pass it to the frontend:
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.
All hooks named useRealtime*
are Realtime hooks, and all hooks named use*
are SWR hooks.
Realtime hooks
useRealtimeRun
The useRealtimeRun
hook allows you to subscribe to a run by its ID.
To correctly type the run’s payload and output, you can provide the type of your task to the useRealtimeRun
hook:
See our Realtime documentation for more information about the type of the run object and more.
useRealtimeRunsWithTag
The useRealtimeRunsWithTag
hook allows you to subscribe to multiple runs with a specific tag.
To correctly type the runs payload and output, you can provide the type of your task to the useRealtimeRunsWithTag
hook:
If useRealtimeRunsWithTag
could return multiple different types of tasks, you can pass a union of all the task types to the hook:
See our Realtime documentation for more information.
useRealtimeBatch
The useRealtimeBatch
hook allows you to subscribe to a batch of runs by its the batch ID.
See our Realtime documentation for more information.
useRealtimeRunWithStreams
The useRealtimeRunWithStreams
hook allows you to subscribe to a run by its ID and also receive any streams that are emitted by the task. See our Realtime documentation for more information about emitting streams from a task.
You can provide the type of the streams to the useRealtimeRunWithStreams
hook:
As you can see above, each stream is an array of the type you provided, keyed by the stream name. If instead of a pure text stream you have a stream of objects, you can provide the type of the object:
Common options
enabled
You can pass the enabled
option to the Realtime hooks to enable or disable the subscription.
This allows you to conditionally disable using the hook based on some state.
id
You can pass the id
option to the Realtime hooks to change the ID of the subscription.
This allows you to change the ID of the subscription based on some state. Passing in a different ID will unsubscribe from the current subscription and subscribe to the new one (and remove any cached data).
experimental_throttleInMs
The *withStreams
variants of the Realtime hooks accept an experimental_throttleInMs
option to throttle the updates from the server. This can be useful if you are getting too many updates and want to reduce the number of updates.
SWR Hooks
useRun
The useRun
hook allows you to fetch a run by its ID.
The 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:
Common options
You can pass the following options to the all SWR hooks:
Revalidate the data when the window regains focus.
Revalidate the data when the browser regains a network connection.
Poll for updates at the specified interval (in milliseconds). Polling is not recommended for most use-cases. Use the Realtime hooks instead.
Common return values
An error object if an error occurred while fetching the data.
A boolean indicating if the data is currently being fetched.
A boolean indicating if the data is currently being revalidated.
A boolean indicating if an error occurred while fetching the data.
Trigger Hooks
We provide a set of hooks that can be used to trigger tasks from your frontend application. You’ll need to generate a Public Access Token with write
permissions to use these hooks. See our frontend guide for more information.
useTaskTrigger
The useTaskTrigger
hook allows you to trigger a task from your frontend application.
useRealtimeTaskTrigger
The useRealtimeTaskTrigger
hook allows you to trigger a task from your frontend application and then subscribe to the run in using Realtime:
useRealtimeTaskTriggerWithStreams
The useRealtimeTaskTriggerWithStreams
hook allows you to trigger a task from your frontend application and then subscribe to the run in using Realtime, and also receive any streams that are emitted by the task.