Skip to content

Commit

Permalink
Merge pull request #829 from Orfium/fix/NDS-1013_table_doesnt_rerende…
Browse files Browse the repository at this point in the history
…r_when_new_data_are_provided

fix: table not rerendering on data change
  • Loading branch information
kostasdano authored Sep 16, 2024
2 parents 7d4bc9b + f45b33e commit b3fcf2a
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 10 deletions.
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

0 comments on commit b3fcf2a

Please sign in to comment.