Skip to Content
React hooks

React hooks for real-time task updates

Subscribe to run progress, stream AI responses, and trigger tasks from React components using @trigger.dev/react-hooks.

@trigger.dev/react-hooks gives your React components live access to your runs. Subscribe to run progress, stream AI output as it generates, or trigger tasks directly from the browser.

Installation

Install the @trigger.dev/react-hooks package in your project:

npm add @trigger.dev/react-hooks
pnpm add @trigger.dev/react-hooks
yarn add @trigger.dev/react-hooks

Authentication

All hooks require authentication with a Public Access Token. Pass the token via the accessToken option:

import {  } from "@trigger.dev/react-hooks";

export function MyComponent({
  runId,
  publicAccessToken,
}: {
  runId: string;
  publicAccessToken: string;
}) {
  const { run, error } = (runId, {
    : publicAccessToken,
    : "https://your-trigger-dev-instance.com", // optional, only needed if you are self-hosting Trigger.dev
  });

  // ...
}

Set the token once for many hooks

To share one token across many hooks, wrap them in TriggerAuthContext.Provider. Hooks under the provider can leave out accessToken:

"use client";

import { ,  } from "@trigger.dev/react-hooks";

export function TriggerProvider({
  publicAccessToken,
  children,
}: {
  publicAccessToken: string;
  children: React.;
}) {
  return (
    <.Provider value={{ : publicAccessToken }}>
      {children}
    </.Provider>
  );
}

function Runs({ tag }: { tag: string }) {
  const { runs } = (tag); // Uses the provider's token

  return <div>{runs.} runs</div>;
}

export function Page({ publicAccessToken }: { publicAccessToken: string }) {
  return (
    <TriggerProvider publicAccessToken={publicAccessToken}>
      <Runs tag="user:123" />
    </TriggerProvider>
  );
}

An accessToken passed to a hook overrides the provider's. A hook that gets no token from either throws when it renders. Every hook that calls the Trigger.dev API, except useRun, accepts enabled, and doesn't throw while you pass enabled: false.

Learn more about generating and managing tokens in our authentication guide.

Available hooks

Hook categoryWhat it doesGuide
Trigger hooksTrigger tasks from the browserTriggering
Run updatesSubscribe to run status, metadata, and tagsRun updates
StreamingConsume AI output, file chunks, or any continuous dataStreaming
SWR hooksOne-time fetch with caching (not recommended for most cases)SWR

SWR vs Realtime hooks

We offer two "styles" of hooks: SWR and Realtime. SWR hooks use the swr library to fetch data once and cache it. Realtime hooks use Trigger.dev Realtime to subscribe to updates as they happen.

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.

Was this page helpful?

Last updated

Edit on GitHub

On this page