Skip to content
Source
Sign inSign up
TanStack/table

alpha: `useTable` causes "Cannot update a component (SubscribeBound) while rendering" after setOptions moved to render body #6224

Closedd3lay(GitHub profile, opens in a new tab) opened this issue 6 comments

TanStack Table version

9.0.0-alpha.20 through alpha.23

Framework/Library version

React v19.2.4

Describe the bug and the steps to reproduce it

Since alpha.20 (cd97cfd), useTable calls table.setOptions() during render instead of in useEffect. This synchronously flushes store subscribers — including SubscribeBound — causing React to warn:

Cannot update a component (`SubscribeBound`) while rendering a different component (`Table`).

Minimal reproduction

import { useState } from 'react'
import { type ColumnDef, tableFeatures, useTable, globalFilteringFeature } from '@tanstack/react-table'

const _features = tableFeatures({ globalFilteringFeature })
const columns: ColumnDef<typeof _features, { id: string; name: string }>[] = [
	{ accessorKey: 'name', header: 'Name' },
]
const data = [{ id: '1', name: 'Alice' }]

function Table({ globalFilter }: { globalFilter: string }) {
	const table = useTable({
		_features,
		data,
		columns,
		state: { globalFilter },
	})
	return (
		<table.Subscribe selector={s => ({ globalFilter: s.globalFilter })}>
			{() => <span>ok</span>}
		</table.Subscribe>
	)
}

export default function App() {
	const [n, setN] = useState(0)
	return (
		<>
			<button onClick={() => setN(c => c + 1)}>Increment ({n})</button>
			<Table globalFilter={`filter-${n}`} />
		</>
	)
}

Open the console, click "Increment" — warning appears on first click.

Possible Cause

setOptions → optionsStore.setState() → atom.set() → propagate + flush() — all synchronous during render. The flush runs subscription effects that call table.store.get() (recomputes, always a new object from spread), which notifies SubscribeBound's useSyncExternalStoreWithSelector listener — a setState call during another component's render phase.

Versions

  • @tanstack/react-table: 9.0.0-alpha.20 through alpha.23
  • @tanstack/store: 0.9.3
  • react: 19.2.4

https://codesandbox.io/p/sandbox/pfm358

Screenshots or Videos (Optional)

No response

Do you intend to try to help solve this bug with your own PR?

No, because I do not know how

Terms & Code of Conduct

  • I agree to follow this project's Code of Conduct
  • I understand that if my bug cannot be reliable reproduced in a debuggable environment, it will probably not be fixed and this issue may even be closed.
  1. I'm a bit conflicted on how to solve this. Any delay in options setting during render like how we have it now in the v9 alpha branch and even the v8 branch seems to cause issues where what is rendered is not accurate and it's "a render behind".

    FYI to @riccardoperra on this one. I could be wrong, but this might just be because of the new optionsStore, which could be ommited in the react/preact adapters potentially?

  2. KevinVandy added labelsv9

  3. riccardoperracommented

    @KevinVandy what was the reason of why we did opt-out for moving setOptions into useEffect? This seems resolve the issue but I am sure there was a reason for that

    useEffect(() => {
      table.setOptions((prev) => ({
        ...prev,
        ...tableOptions,
      }))
    }, [tableOptions])
  4. Last I tried, columns and data can get "1 render behind".

    And if we try to address that by subscribing to the optionsStore to force an extra rerender, it can cause performance issues since shallow compare would need to run on data.

  5. @KevinVandy What if we do this instead ?

    useEffect(() => {
      table.setOptions((prev) => ({
        ...prev,
        ...tableOptions,
      }))
    })

    useEffect with no dependencies param, which will cause the callback to be ran for every render ?

  6. We hit this on 9.0.0-beta.9 in a production app and can add two data points that may help scope the fix.

    1. The warning fires even without table.Subscribe — the hook's own default selector subscription is enough.

    Any table with controlled state (state: { columnFilters } + change handlers, etc.) warns on every state change, because useTable's internal useSelector(table.store, selector) is itself a store subscriber. Call stack captured from the warning on beta.9:

    table_syncExternalStateToBaseAtoms   (@tanstack/table-core)
      → _reactivity.batch → flush → notify   (@tanstack/store)
      → useSyncExternalStoreWithSelector handleStoreChange → setState   ⚠️ during render

    Sequence: an event handler updates the controlled slice → re-render → useTable runs table.setOptions(...) in the render body → table_syncExternalStateToBaseAtoms writes the changed slice to its base atom → the store notifies synchronously → the hook's own subscription (and any Subscribe) setStates mid-render.

    So while #6244's options-store removal may have helped the original SubscribeBound report, the external-state sync leg still reproduces on beta.9, and the relevant code is unchanged in beta.10 (useTable.ts still syncs options in the render body; coreTablesFeature.utils.ts still calls table_syncExternalStateToBaseAtoms from table_setOptions).

    2. There's a second, independent render-phase setState source with controlled pagination: autoResetPageIndex.

    createCoreRowModel / createFilteredRowModel / createSortedRowModel wire onAfterUpdate: () => table_autoResetPageIndex(table), and those memos recompute lazily — typically inside render when something first calls getRowModel() after a filter/sort change. table_autoResetPageIndex then calls table_resetPageIndex synchronously, which routes through options.onPaginationChange → a React state setter mid-render. The expanded feature's equivalent already schedules its reset:

    // rowExpandingFeature.utils.ts
    table._reactivity.schedule(() => table_resetExpanded(table))
    
    // rowPaginationFeature.utils.ts — not scheduled
    table_resetPageIndex(table)

    Routing the pagination auto-reset through _reactivity.schedule(...) the same way looks like a one-line consistency fix — happy to PR that if you'd take it. (This leg would keep warning even if setOptions moved to an effect.)

    Workarounds we're shipping meanwhile, in an app-level hook wrapper, in case they're useful to others: pass a constant selector (() => null) when nothing reads table.state — the subscription compares equal and never re-renders — and defer onPaginationChange by a microtask. Both verified to remove the warnings with no visible behavior change (microtasks run before paint), with all table state parent-controlled.

  7. we think we finally fixed this in the latest v9 beta

  8. KevinVandy closed this

Sign in to join this conversation.