Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
6580796
fix: headers in table state to fix desync w/ rows
benjamin-t-santos Aug 25, 2026
19e48da
fix: revert white space changes
benjamin-t-santos Aug 25, 2026
c5af495
Merge branch 'main' into data-table-header-flicker-fix
benjamin-t-santos Aug 26, 2026
3513cc4
fix: compare complete headers (not just keys)
benjamin-t-santos Aug 27, 2026
525c187
Merge branch 'main' into data-table-header-flicker-fix
benjamin-t-santos Aug 27, 2026
116212e
fix: use copy of headers for data table state
benjamin-t-santos Aug 31, 2026
30e3f67
fix: create new headers elements for states, better test coverage
benjamin-t-santos Aug 31, 2026
2a9ca56
Merge branch 'main' into data-table-header-flicker-fix
benjamin-t-santos Sep 2, 2026
2e5e290
Merge branch 'main' into data-table-header-flicker-fix
benjamin-t-santos Sep 8, 2026
13c5967
Merge branch 'main' into data-table-header-flicker-fix
benjamin-t-santos Sep 14, 2026
b07d255
Update getDerivedStateFromProps-test.js
benjamin-t-santos Oct 27, 2026
f2eb65a
Merge remote-tracking branch 'upstream/main' into data-table-header-f…
benjamin-t-santos Nov 3, 2026
a11b94c
fix: add coverage for updated derived props via mutation
benjamin-t-santos Nov 3, 2026
3b8c861
chore: verify DataTable render props use same header/row versions
benjamin-t-santos Nov 5, 2026
38431a8
Merge branch 'main' into data-table-header-flicker-fix
benjamin-t-santos Nov 5, 2026
db902f9
fix: use length for last call
benjamin-t-santos Nov 5, 2026
7ca9f41
Merge branch 'main' into data-table-header-flicker-fix
benjamin-t-santos Nov 10, 2026
a9c5891
Merge branch 'main' into data-table-header-flicker-fix
kennylam Nov 11, 2026
5757a0a
Merge branch 'main' into data-table-header-flicker-fix
benjamin-t-santos Nov 11, 2026
8b02e06
Merge branch 'main' into data-table-header-flicker-fix
heloiselui Nov 13, 2026
229ff18
Merge branch 'main' into data-table-header-flicker-fix
benjamin-t-santos Nov 16, 2026
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
11 changes: 4 additions & 7 deletions packages/react/src/components/DataTable/DataTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ interface DataTableState<ColTypes extends any[]> {
isExpandedAll: boolean;
rowIds: string[];
rowsById: Record<string, DataTableRow<ColTypes>>;
headers: DataTableHeader[];
shouldShowBatchActions: boolean;
sortDirection: DataTableSortState;
sortHeaderKey: string | null;
Expand Down Expand Up @@ -379,12 +380,8 @@ export const DataTable = <RowType, ColTypes extends any[]>(

useEffect(() => {
const nextRowIds = rows.map((row) => row.id);
const nextHeaders = headers.map((header) => header.key);
const hasRowIdsChanged = !isEqual(nextRowIds, state.rowIds);
const currentHeaders = Array.from(
new Set(Object.keys(state.cellsById).map((id) => id.split(':')[1]))
);
const hasHeadersChanged = !isEqual(nextHeaders, currentHeaders);
const hasHeadersChanged = !isEqual(headers, state.headers);
Comment thread
adamalston marked this conversation as resolved.
Resolved
const currentRows = state.rowIds.map((id) => {
const row = state.rowsById[id];
return headers.reduce(
Expand Down Expand Up @@ -664,7 +661,7 @@ export const DataTable = <RowType, ColTypes extends any[]>(
? filterRows({
cellsById: state.cellsById,
getCellId,
headers,
headers: state.headers,
inputValue: state.filterInputValue,
rowIds: state.rowIds,
})
Expand Down Expand Up @@ -860,7 +857,7 @@ export const DataTable = <RowType, ColTypes extends any[]>(
const renderProps: RenderProps = {
// Data derived from state
rows: denormalize(filteredRowIds, state.rowsById, state.cellsById),
headers: headers,
headers: state.headers,
selectedRows: denormalize(selectedRows, state.rowsById, state.cellsById),

// Prop accessors/getters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1206,6 +1206,83 @@ describe('DataTable', () => {
'Field 3:A!',
]);
});

it('should never use mismatched headers and rows for render props while syncing new props', () => {
const childrenSpy = jest.fn(() => null);
const initialHeaders = [
{ key: 'fieldA', header: 'Field A' },
{ key: 'fieldB', header: 'Field B' },
];
const initialRows = [
{ id: 'a', fieldA: 'A:1', fieldB: 'B:1' },
{ id: 'b', fieldA: 'A:2', fieldB: 'B:2' },
];
const nextHeaders = [
{ key: 'fieldB', header: 'Field B' },
{ key: 'fieldC', header: 'Field C' },
];
const nextRows = [
{ id: 'a', fieldB: 'B:1', fieldC: 'C:1' },
{ id: 'b', fieldB: 'B:2', fieldC: 'C:2' },
{ id: 'c', fieldB: 'B:3', fieldC: 'C:3' },
];

const { rerender } = render(
<DataTable headers={initialHeaders} rows={initialRows}>
{childrenSpy}
</DataTable>
);

// Every render must use headers and rows from the same version:
// each row has exactly one cell per header, in header order.
const expectCellsToMatchHeaders = ({ headers, rows }) => {
const headerKeys = headers.map((header) => header.key);
rows.forEach((row) => {
expect(row.cells.map((cell) => cell.info.header)).toEqual(
headerKeys
);
});
};

childrenSpy.mock.calls.forEach(([renderProps]) =>
expectCellsToMatchHeaders(renderProps)
);
childrenSpy.mockClear();

rerender(
<DataTable headers={nextHeaders} rows={nextRows}>
{childrenSpy}
</DataTable>
);

const calls = childrenSpy.mock.calls.map(
([renderProps]) => renderProps
);

// The render that happens before the sync effect still exposes the
// previous version of both headers and rows.
expect(calls.length).toBeGreaterThan(1);
expect(calls[0].headers.map((header) => header.key)).toEqual([
'fieldA',
'fieldB',
]);
expect(calls[0].rows.map((row) => row.id)).toEqual(['a', 'b']);

calls.forEach(expectCellsToMatchHeaders);

// The render that happens after the sync effect still uses the
// new versions of both headers and rows.
const lastCall = calls[calls.length - 1];
expect(lastCall.headers.map((header) => header.key)).toEqual([
'fieldB',
'fieldC',
]);
expect(lastCall.rows.map((row) => row.id)).toEqual(['a', 'b', 'c']);
expect(lastCall.rows[2].cells.map((cell) => cell.value)).toEqual([
'B:3',
'C:3',
]);
});
});

describe('row click behavior', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,48 @@ describe('getDerivedStateFromProps', () => {
);
});

it('stores headers from props in state', () => {
const headers = [{ key: 'name', header: 'Name' }];
const props = { rows: [], headers };
const state = getDerivedStateFromProps(props, {});
expect(state.headers).toEqual(headers);
});

it('updates headers in state when props.headers changes', () => {
const headersA = [{ key: 'name', header: 'Name' }];
const headersB = [
{ key: 'col1', header: 'Column 1' },
{ key: 'col2', header: 'Column 2' },
];
const stateA = getDerivedStateFromProps(
{ rows: [], headers: headersA },
{}
);
expect(stateA.headers).toEqual(headersA);
const stateB = getDerivedStateFromProps(
{ rows: [], headers: headersB },
stateA
);
expect(stateB.headers).toEqual(headersB);
});
Comment thread
heloiselui marked this conversation as resolved.
Resolved

it('should copy headers so that prop mutations do not affect already-derived state', () => {
const headersA = [{ key: 'name', header: 'Name' }];
const stateA = getDerivedStateFromProps(
{ rows: [], headers: headersA },
{}
);
expect(stateA.headers).not.toBe(headersA);
expect(stateA.headers[0]).not.toBe(headersA[0]);
headersA[0].key = 'newKey';
expect(stateA.headers[0].key).toEqual('name');
const stateB = getDerivedStateFromProps(
{ rows: [], headers: headersA },
stateA
);
expect(stateB.headers[0].key).toEqual('newKey');
});
Comment thread
benjamin-t-santos marked this conversation as resolved.
Resolved

describe('with previous state', () => {
let mockProps;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const getDerivedStateFromProps = (props, prevState) => {
);
const hasSelectedRows = rowIds.some((id) => isSelectedRow(rowsById[id]));
const state = {
headers: props.headers.map((header) => ({ ...header })),
rowIds,
rowsById,
cellsById,
Expand Down
Loading