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
`useTableContext` reads the React table context supplied by the `<table.AppTable>` provider component. When no provider exists above the calling component, the context is undefined and the library throws this error with instructions to wrap the component in `<table.AppTable>...</table.AppTable>`. It guards against using table APIs without an actual table instance.
Source
Thrown at packages/react-table/src/createTableHook.tsx:801
*/
function useTableContext<
TData extends RowData = RowData,
TSelected = TableState<TFeatures>,
>(): AppReactTable<
TFeatures,
TData,
TSelected,
TTableComponents,
TCellComponents,
THeaderComponents
> {
// `useContext` keeps React 18 support; `use(Context)` is React 19+ only.
// eslint-disable-next-line @eslint-react/no-use-context -- intentional for React 18
const table = useContext(TableContext)
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (!table) {
throw new Error(
'`useTableContext` must be used within an `AppTable` component. ' +
'Make sure your component is wrapped with `<table.AppTable>...</table.AppTable>`.',
)
}
// The value provided by `<table.AppTable>` is the extended table (the App*
// wrapper components and `tableComponents` are Object.assign-ed onto the same
// instance `useAppTable` returns), so this asserts the runtime shape.
return table as unknown as AppReactTable<
TFeatures,
TData,
TSelected,
TTableComponents,
TCellComponents,
THeaderComponents
>
}
View on GitHub (pinned to d01c01bedb)
Solutions
- Wrap the consuming component with `<table.AppTable table={table}>...</table.AppTable>` so the context is provided.
- Render the component inside AppTable's children instead of beside it.
- Dedupe the table package (npm ls, package overrides) to ensure a single context module instance.
- For optional usage, read `useContext(TableContext)` directly and handle the undefined case.
Example fix
// before
function RowCount() {
const table = useTableContext()
return <span>{table.getFilteredRowModel().rows.length}</span>
}
// after
<table.AppTable table={table}>
<RowCount />
</table.AppTable> Defensive patterns
Strategy: validation
Validate before calling
import { useContext } from 'react'
import { TableContext } from '@your-scope/react-table'
function useSafeTable() {
const table = useContext(TableContext)
if (!table) {
console.error('useTableContext requires <table.AppTable> above this component')
}
return table
} Type guard
function hasTable<T>(t: T | null | undefined): t is T {
return t != null
} Try / catch
try {
const table = useTableContext()
// use table
} catch (err) {
if (err instanceof Error && err.message.includes('useTableContext')) {
return null // not inside <table.AppTable>
}
throw err
} Prevention
- Render table-consuming components only inside <table.AppTable> children.
- Avoid portals or separate ReactDOM roots for table children.
- Dedupe react-table versions in the lockfile.
- Provide a test helper that wraps subjects in AppTable.
When it happens
Trigger: Calling `useTableContext()` from a component rendered outside `<table.AppTable>`; rendering a consumer through a portal or a separate React root; multiple installed copies of react-table creating divergent contexts.
Common situations: A custom row/cell toolbar component using table-level APIs placed outside AppTable; storybook or test harness mounting only the child component; version duplication after dependency bumps.
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/d9253f7c1366f73e.
Report an issue: GitHub.