TanStack/table · error

`useTableContext` must be used within an `AppTable` componen

Error message

`useTableContext` must be used within an `AppTable` component. Make sure your component is wrapped with `<table.AppTable>...</table.AppTable>`.

What it means

Svelte's useTableContext reads the table from Svelte context (getContext(tableContextKey)), which is only set by the <table.AppTable> component. Outside that provider the context is undefined, so the hook throws this error.

Source

Thrown at packages/svelte-table/src/createTableHook.svelte.ts:541

    >
  }

  /**
   * Access the table instance from within an `AppTable` wrapper.
   * Use this in custom `tableComponents` passed to `createTableHook`.
   * TFeatures is already known from the createTableHook call.
   */
  function useTableContext<TData extends RowData = RowData>(): AppSvelteTable<
    TFeatures,
    TData,
    TTableComponents,
    TCellComponents,
    THeaderComponents
  > {
    const table = getContext(tableContextKey)

    if (!table) {
      throw new Error(
        '`useTableContext` must be used within an `AppTable` component. ' +
          'Make sure your component is wrapped with `<table.AppTable>...</table.AppTable>`.',
      )
    }

    // `<table.AppTable>` provides the extended table (the App* wrapper
    // components and `tableComponents` are Object.assign-ed onto the same
    // instance `createAppTable` returns), so this asserts the runtime shape.
    return table as unknown as AppSvelteTable<
      TFeatures,
      TData,
      TTableComponents,
      TCellComponents,
      THeaderComponents
    >
  }

  /**

View on GitHub (pinned to d01c01bedb)

Solutions

  1. Wrap the consuming component with <table.AppTable>...</table.AppTable>
  2. Pass the table instance as a prop instead of relying on context
  3. Ensure the hook is called during component initialization (not in async callbacks or events after teardown)

Example fix

<!-- before -->
<script>const table = useTableContext()</script> <!-- throws -->
<!-- after -->
<table.AppTable>
  <MyBody /> <!-- useTableContext() works here -->
</table.AppTable>
Defensive patterns

Strategy: validation

Validate before calling

const table = getContext(tableContextKey);
if (!table) throw new Error('useTableContext must be used within <table.AppTable>');

Type guard

function isTableContext(t: unknown): t is Table {
  return !!t && typeof t === 'object' && 'getRowModel' in t;
}

Try / catch

try {
  const table = useTableContext();
} catch (e) {
  if (e.message.includes('AppTable')) {
    // restructure so the component renders inside AppTable
  } else { throw e; }
}

Prevention

When it happens

Trigger: Calling useTableContext() in a Svelte component not nested under <table.AppTable>; using it at module top level instead of during component initialization; rendering in portals/snippets outside the provider subtree.

Common situations: Svelte 5 snippet/child components rendered outside the AppTable wrapper; moving table-related components into a separate component tree without re-providing context.

Related errors


AI-assisted analysis of TanStack/table@d01c01bedb (2026-08-28). Data as JSON: /api/errors/d637e7cbd3b02727. Report an issue: GitHub.