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
- Wrap the consuming component with <table.AppTable>...</table.AppTable>
- Pass the table instance as a prop instead of relying on context
- 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
- Ensure Svelte context consumers are nested under <table.AppTable> (getContext only sees ancestor providers)
- Call the hook during component initialization, not in events/async code
- Use snippet children of AppTable rather than detached component trees
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
- `useCellContext` must be used within an `AppCell` component.
- `useHeaderContext` must be used within an `AppHeader` or `Ap
- `useTableContext` must be used within an `AppTable` componen
- `useCellContext` must be used within an `AppCell` component.
- `useHeaderContext` must be used within an `AppHeader` or `Ap
AI-assisted analysis of TanStack/table@d01c01bedb (2026-08-28).
Data as JSON: /api/errors/d637e7cbd3b02727.
Report an issue: GitHub.