Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: table not rerendering on data change #829

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/components/Table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ const Table = <TData extends NoUndefined<TData>>({
sorting,
rowsConfig,
columnsConfig,
pagination,
dataTestPrefixId,
});

Expand Down
12 changes: 3 additions & 9 deletions src/components/Table/hooks/useTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from '@tanstack/react-table';
import useTheme from 'hooks/useTheme';
import { concat } from 'lodash-es';
import React from 'react';
import React, { useMemo } from 'react';
import type { Theme } from 'theme';

import { CheckBox } from 'components/Controls';
Expand Down Expand Up @@ -152,7 +152,6 @@ const useTable = <TData,>({
sorting,
rowsConfig,
columnsConfig,
pagination,
dataTestPrefixId,
...rest
}): ReturnValue<TData> => {
Expand All @@ -164,6 +163,8 @@ const useTable = <TData,>({

const hasRowDetails = data.some((row) => row.details) && Boolean(expanded);

const tableData = useMemo(() => data.map((data) => data.cells), [data]);

const hasCheckboxes = Boolean(rowSelection && isTableInteractive);

const tColumns = getColumns(columns, hasCheckboxes, hasRowDetails, theme, dataTestPrefixId);
Expand All @@ -177,13 +178,6 @@ const useTable = <TData,>({
};
}, [columnsConfig, expanded, isTableInteractive, rowSelection, sorting]);

/** Since tanstack's useTable doesn't detect array object mutations, we need to manually create new data array (new ref) to trigger a re-render */
const [tableData, setTableData] = React.useState(data.map((data) => data.cells));

React.useEffect(() => {
setTableData(data.map((data) => data.cells));
}, [sorting?.sortingColumn, pagination?.page, pagination?.showItems]);

const table = useReactTable<TData>({
/** Basic Functionality */
data: tableData,
Expand Down
Loading