Skip to main content

Overview

useTamboThreadList is a React Query hook for fetching a paginated list of threads with automatic caching and refetching.

Import

import { useTamboThreadList } from '@tambo-ai/react';

Signature

function useTamboThreadList(
  listOptions?: ThreadListParams,
  queryOptions?: UseQueryOptions<ThreadListResponse>
): UseQueryResult<ThreadListResponse>

Parameters

listOptions
ThreadListParams
Filtering and pagination options
queryOptions
UseQueryOptions<ThreadListResponse>
Additional React Query options

Return Value

data
ThreadListResponse
The thread list response
isLoading
boolean
Whether the initial data is being loaded
isError
boolean
Whether an error occurred
error
Error | null
Error object if the query failed
refetch
() => Promise<void>
Function to manually refetch the list

Behavior

  • Stale Time: Thread list is considered stale after 5 seconds
  • User Context: Automatically uses userKey from TamboProvider if not explicitly provided
  • Authentication: Automatically disabled until user is identified
  • Pagination: Supports cursor-based pagination for efficient loading

Examples

Basic Usage

import { useTamboThreadList } from '@tambo-ai/react';

function ThreadList() {
  const { data, isLoading, isError } = useTamboThreadList({
    limit: 20,
  });

  if (isLoading) {
    return <div>Loading threads...</div>;
  }

  if (isError) {
    return <div>Error loading threads</div>;
  }

  return (
    <ul>
      {data.threads.map(thread => (
        <li key={thread.id}>
          <h3>{thread.name || 'Untitled'}</h3>
          <p>Status: {thread.runStatus}</p>
          <time>{new Date(thread.createdAt).toLocaleDateString()}</time>
        </li>
      ))}
    </ul>
  );
}

With Pagination

import { useState } from 'react';
import { useTamboThreadList } from '@tambo-ai/react';

function PaginatedThreadList() {
  const [cursor, setCursor] = useState<string | undefined>();
  
  const { data, isLoading } = useTamboThreadList({
    limit: 10,
    cursor,
  });

  if (isLoading) return <Spinner />;

  return (
    <div>
      <ul>
        {data.threads.map(thread => (
          <ThreadItem key={thread.id} thread={thread} />
        ))}
      </ul>
      
      {data.hasMore && (
        <button onClick={() => setCursor(data.nextCursor!)}>
          Load More
        </button>
      )}
    </div>
  );
}

For Specific User

Override the context userKey:
function UserThreadList({ userId }: { userId: string }) {
  const { data, isLoading } = useTamboThreadList({
    userKey: userId, // Explicit userKey
    limit: 50,
  });

  if (isLoading) return <Spinner />;

  return (
    <div>
      <h2>Threads for {userId}</h2>
      <ThreadGrid threads={data.threads} />
    </div>
  );
}

With Sorting

function SortedThreadList() {
  const [order, setOrder] = useState<'asc' | 'desc'>('desc');
  
  const { data, isLoading } = useTamboThreadList({
    order,
    limit: 20,
  });

  return (
    <div>
      <select value={order} onChange={e => setOrder(e.target.value as 'asc' | 'desc')}>
        <option value="desc">Newest First</option>
        <option value="asc">Oldest First</option>
      </select>

      {isLoading ? (
        <Spinner />
      ) : (
        <ThreadList threads={data.threads} />
      )}
    </div>
  );
}

Thread Sidebar

function ThreadSidebar() {
  const [selectedId, setSelectedId] = useState<string>();
  const { data, isLoading } = useTamboThreadList({ limit: 100 });

  if (isLoading) return <SidebarSkeleton />;

  return (
    <aside>
      <h2>Conversations</h2>
      <ul>
        {data.threads.map(thread => (
          <li
            key={thread.id}
            className={selectedId === thread.id ? 'selected' : ''}
            onClick={() => setSelectedId(thread.id)}
          >
            <span>{thread.name || 'New conversation'}</span>
            <time>{formatRelativeTime(thread.updatedAt)}</time>
          </li>
        ))}
      </ul>
    </aside>
  );
}

useTamboThread

Fetch a single thread

useTambo

Main hook for thread state

Thread Types

Thread type definitions

Threads Concept

Learn about threads

Build docs developers (and LLMs) love