Demo
We’ve created a Demo application that demonstrates how to use our React hooks to trigger tasks in a Next.js application. The application uses the@trigger.dev/react-hooks
package to trigger a task and subscribe to the run in real-time.
Installation
Install the@trigger.dev/react-hooks
package in your project:
Authentication
To authenticate a trigger hook, you must provide a special one-time use “trigger” token. These tokens are very similar to Public Access Tokens, but they can only be used once to trigger a task. You can generate a trigger token using theauth.createTriggerPublicToken
function in your backend code:
expirationTime
parameter:
createTriggerPublicToken
function to create a token that can trigger multiple tasks:
multipleUse
parameter to create a token that can be used multiple times:
After generating the trigger token in your backend, you must pass it to your frontend application.
We have a guide on how to do this in the React hooks
overview.
Hooks
useTaskTrigger
TheuseTaskTrigger
hook allows you to trigger a task from your frontend application.
useTaskTrigger
returns an object with the following properties:
submit
: A function that triggers the task. It takes the payload of the task as an argument.handle
: The run handle object. This object contains the ID of the run that was triggered, along with a Public Access Token that can be used to access the run.isLoading
: A boolean that indicates whether the task is currently being triggered.error
: An error object that contains any errors that occurred while triggering the task.
submit
function triggers the task with the specified payload. You can additionally pass an optional options argument to the submit
function:
Using the handle object
You can use thehandle
object to initiate a subsequent realtime hook to subscribe to the run.
useRealtimeTaskTrigger
TheuseRealtimeTaskTrigger
hook allows you to trigger a task from your frontend application and then subscribe to the run in using Realtime:
useRealtimeTaskTriggerWithStreams
TheuseRealtimeTaskTriggerWithStreams
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.