alpha: `useTable` causes "Cannot update a component (SubscribeBound) while rendering" after setOptions moved to render body #6224
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.3react: 19.2.4
Your Minimal, Reproducible Example - (Sandbox Highly Recommended)#
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.
KevinVandy added labelsv9
@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])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.
@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 ?
We hit this on
9.0.0-beta.9in 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, becauseuseTable's internaluseSelector(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 renderSequence: an event handler updates the controlled slice → re-render →
useTablerunstable.setOptions(...)in the render body →table_syncExternalStateToBaseAtomswrites the changed slice to its base atom → the store notifies synchronously → the hook's own subscription (and anySubscribe) setStates mid-render.So while #6244's options-store removal may have helped the original
SubscribeBoundreport, the external-state sync leg still reproduces on beta.9, and the relevant code is unchanged in beta.10 (useTable.tsstill syncs options in the render body;coreTablesFeature.utils.tsstill callstable_syncExternalStateToBaseAtomsfromtable_setOptions).2. There's a second, independent render-phase setState source with controlled pagination:
autoResetPageIndex.createCoreRowModel/createFilteredRowModel/createSortedRowModelwireonAfterUpdate: () => table_autoResetPageIndex(table), and those memos recompute lazily — typically inside render when something first callsgetRowModel()after a filter/sort change.table_autoResetPageIndexthen callstable_resetPageIndexsynchronously, which routes throughoptions.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 ifsetOptionsmoved 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 readstable.state— the subscription compares equal and never re-renders — and deferonPaginationChangeby a microtask. Both verified to remove the warnings with no visible behavior change (microtasks run before paint), with all table state parent-controlled.we think we finally fixed this in the latest v9 beta
KevinVandy closed this
Sign in to join this conversation.
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?