table: Can't perform a React state update on a component that hasn't mounted yet
Describe the bug
Error received when navigating to a table using getGroupedRowModel() such as:
const table = useReactTable({
data,
columns,
getCoreRowModel: getCoreRowModel(),
getGroupedRowModel: getGroupedRowModel(),
});
As seen in this example, navigating to the basic table works fine where as navigating to the grouped table produces an error in the console. The only difference between the two is getGroupedRowModel().
I am using the code from the examples pretty much as is (stripped down for simplicity).
Your minimal, reproducible example
https://github.com/tacomanator/tanstack-table-unmounted
Steps to reproduce
- Clone example, install dependencies, and run
- While viewing the console, navigate to the grouped table
- Observe error
Expected behavior
No error output
How often does this bug happen?
Every time
Screenshots or Videos
https://github.com/TanStack/table/assets/63794/30edbbcf-8f7e-459f-b624-b7d82aac3f0a
Platform
macOS, Chrome 115.0.5790.170 arm64
react-table version
8.9.3
TypeScript version
5.1.6
Additional context
No response
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.
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 35
- Comments: 29
adding this to hook
useReactTablesolved the issue for me:I think it has something to do with pagination. When you add
getGroupedRowModelit ends up calling setPagination in the render and that ends up callingsetState.So if you set
onPaginationChangein theuseReactTabledefinition toonPaginationChange: () => undefinedthen the error also goes away.For anyone stumbling upon this in 2025, you probably don’t want to turn off
autoResetPageIndex(unless you are manually paging) and instead should go with the fix suggested by @nabilfatih https://github.com/TanStack/table/issues/5026#issuecomment-2315023555 until PR #5879 is acceptedI like capturing that logic in a component:
just add:
This also happens on
getFacetedUniqueValuesI am having the same problem in version 8.9.1 when I call table.getRowModel() to conditionally render rows in tbody and I can confirm that it probably has something to do with getGroupedRowModel.
If i remove getGroupedRowModel: getGroupedRowModel() from the hook the error disappears.
I think this is due to the fact that these errors don’t show up in production builds but still happen.
Happy to be proven wrong.
Correct link to PR: https://github.com/TanStack/table/pull/5879. Wondering why there are no comments on it…
same issue in getFacetedUniqueValues
As @vktrl mentioned, this occurs in strict mode because React mounts and unmounts the component.
The RowPagination feature will make a
setStatecall (from callingresetPageIndex) ifautoResetPageIndexisTrue, which is default (see here)I have a PR to fix this by tracking whether the component is mounted or not and only calling setState if it is
https://github.com/TanStack/table/pull/5879/commits/f02386302c429937cc2f38ad7342335bf3ee516b
I faced a similar issue. in my case, the affected functionality is due to function getSortedRowModel() from checking “isEmpty” of my data which this caused the error , my temporary solution is :
getSortedRowModel: !data.isEmpty ? getSortedRowModel() : nullalso facing the same
I’m seeing this exact same issue today.
My error was caused by
getSelectedRowModel()andautoResetPageIndex: falsefixed it.Thank you. It works but I’m wondering why
@ApacheEx
I’m using
table.getSelectedRowModel().rows.lengthI guess that doesn’t help in my case.I’ve been looking for a solution to this problem for a while. After reading further up about the pagination state causing the error I added
manualPagination: trueto theuseReactTablehook props and it seems to fix it.My use case has no need for client side pagination, so I guess it would be ok to set
manualPaginationto true?I receive this error as well. For now I disable
getGroupedRowModeland the error is gone.Using latest version of tanstack @ 8.11.6, getting this when calling
table.getIsAllRowsSelected()in dev mode. I can confirm this still errors when strict mode is off.Temporary fix is to move the header component key into the actual rendering of the table and use an early check on the number of selected elements.