Skip to content

fix(data-table): add headers to table state to prevent brief mismatch with rows (#22714) - #23114

Open
benjamin-t-santos wants to merge 21 commits into
carbon-design-system:mainfrom
benjamin-t-santos:data-table-header-flicker-fix
Open

benjamin-t-santos wants to merge 21 commits into
carbon-design-system:mainfrom
benjamin-t-santos:data-table-header-flicker-fix

Conversation

@benjamin-t-santos

@benjamin-t-santos benjamin-t-santos commented Aug 25, 2026 •

Copy link
Copy Markdown

Closes #22714

Previously, DataTable passed headers directly from its props into the render-prop callback, while rows were derived from internal state. That state is only updated inside a useEffect which runs after the render. If you provided new headers and rows to a DataTable component, on the first render the headers were from the new table, while the rows still displayed the previous values.

Now, headers is stored in the component state, and the render-prop callback is called with the stateful values of both headers and rows. This ensures they always represent the same table, preventing the visible "flicker".

I am open to other approaches/suggestions. Would love to see this fixed; it impacts an IBM product I work on.

Changelog

Changed

DataTable.tsx

  • headers: DataTableHeader[] is now a member of the DataTableState interface
  • renderProps is initialized with state.headers
  • hasHeadersChanged compares props to state

getDerivedStateFromProps.js

  • Now includes a headers field to satisfy the DataTableState interface

getDerivedStateFromProps-test.js

  • Coverage on the new headers field

Testing / Reviewing

https://stackblitz.com/edit/github-6cyyzrpd?file=src%2FApp.jsx This is a StackBlitz example that demonstrates the bug. I adapted it for a new component using Storybook. I set a breakpoint at the start of the useEffect() in DataTable.tsx that determines if the rows or headers change. At that breakpoint, they should not be "desynced". After useEffect runs and the component re-renders, they should both be updated.

PR Checklist

As the author of this PR, before marking ready for review, confirm you:

  • Reviewed every line of the diff
  • Updated documentation and storybook examples - don't think I need to for this fix?
  • Followed the
    required v12 migration documentation
    for any code change that affects v12, or struck through this item because
    the PR does not affect v12
  • Wrote passing tests that cover this change
  • Addressed any impact on accessibility (a11y)
  • Tested for cross-browser consistency
  • Validated that this code is ready for review and status checks should pass

More details can be found in the pull request guide

fix: move headers to table state to prevent desync with rows

a fix for issue 22714.
Previously, the header props were passed directly to the table.
This led to a situation where the table rendered with the new headers,
but old rows (useEffect had not ran yet).
Now, both headers and rows are derived from the Data Table state
@netlify

netlify Bot commented Aug 25, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for v12-carbon-react ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 229ff18
🔍 Latest deploy log https://app.netlify.com/projects/v12-carbon-react/deploys/6afb4b69840e484efca91a3f
😎 Deploy Preview https://deploy-preview-23114--v12-carbon-react.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@netlify

netlify Bot commented Aug 25, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for v12-carbon-web-components ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 229ff18
🔍 Latest deploy log https://app.netlify.com/projects/v12-carbon-web-components/deploys/6afb4b6990896e485e320c2d
😎 Deploy Preview https://deploy-preview-23114--v12-carbon-web-components.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@github-actions

github-actions Bot commented Aug 25, 2026 •

Copy link
Copy Markdown
Contributor

All contributors have signed the DCO.
Posted by the DCO Assistant Lite bot.

fix: revert white space changes

formatter deleted whitespace
want to keep my diff clean
@netlify

netlify Bot commented Aug 25, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for v11-carbon-react ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 229ff18
🔍 Latest deploy log https://app.netlify.com/projects/v11-carbon-react/deploys/6afb4b6968f92bde4b8c78c2
😎 Deploy Preview https://deploy-preview-23114--v11-carbon-react.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@netlify

netlify Bot commented Aug 25, 2026

Copy link
Copy Markdown

✅ Deploy Preview for v11-carbon-web-components ready!

Name Link
🔨 Latest commit 6580796
🔍 Latest deploy log https://app.netlify.com/projects/v11-carbon-web-components/deploys/6a8e043537df1e000833aae7
😎 Deploy Preview https://deploy-preview-23114--v11-carbon-web-components.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@netlify

netlify Bot commented Aug 25, 2026

Copy link
Copy Markdown

✅ Deploy Preview for carbon-elements ready!

Name Link
🔨 Latest commit 6580796
🔍 Latest deploy log https://app.netlify.com/projects/carbon-elements/deploys/6a8e0435105bc40008f14d8d
😎 Deploy Preview https://deploy-preview-23114--carbon-elements.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@netlify

netlify Bot commented Aug 25, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for v11-carbon-web-components ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 229ff18
🔍 Latest deploy log https://app.netlify.com/projects/v11-carbon-web-components/deploys/6afb4b693347d4fe4a2e9e2c
😎 Deploy Preview https://deploy-preview-23114--v11-carbon-web-components.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@netlify

netlify Bot commented Aug 25, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for carbon-elements ready!

Name Link
🔨 Latest commit 229ff18
🔍 Latest deploy log https://app.netlify.com/projects/carbon-elements/deploys/6afb4b69c630d545e8db183c
😎 Deploy Preview https://deploy-preview-23114--carbon-elements.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@benjamin-t-santos
benjamin-t-santos marked this pull request as ready for review August 25, 2026 21:28
@benjamin-t-santos
benjamin-t-santos requested a review from a team as a code owner August 25, 2026 21:28
@benjamin-t-santos

benjamin-t-santos commented Aug 25, 2026 •

Copy link
Copy Markdown
Author

I have read the CLA Document and I hereby sign the CLA.

@benjamin-t-santos

Copy link
Copy Markdown
Author

I have read the DCO document and I hereby sign the DCO.

@codecov

codecov Bot commented Aug 26, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.71%. Comparing base (3be33b4) to head (229ff18).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #23114      +/-   ##
==========================================
- Coverage   90.74%   90.71%   -0.03%     
==========================================
  Files         817      817              
  Lines       79823    79821       -2     
  Branches    10479    10448      -31     
==========================================
- Hits        72432    72409      -23     
- Misses       7192     7207      +15     
- Partials      199      205       +6     
Flag Coverage Δ
main-packages 88.12% <100.00%> (-0.01%) ⬇️
web-components 91.48% <ø> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

fix: do a deep comparison of the state vs incoming headers

comparing by keys is not enough
header can have same keys but slug, decorator, or label
may have changed
@adamalston adamalston mentioned this pull request Aug 27, 2026
3 of 7 tasks
fix: use copy of headers for data table state

to avoid desync between cellsById and header keys when filtering
fix: create new headers elements for states, better test coverage
@benjamin-t-santos

benjamin-t-santos commented Sep 14, 2026 •

Copy link
Copy Markdown
Author

Hi, would it be possible to get this reviewed again? This is an issue that affects an IBM product. I found it when moving from an older Carbon version to the latest. It did not happen on ~1.78.0, so it is a regression. New headers rendering with the older rows is visible:

Screen.Recording.2026-09-03.at.2.45.55.PM.mov

If my current changes are not good, please suggest another approach or a workaround (I have tried many but can't find anything that can prevent the desync)

@adamalston adamalston left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you merge main into your branch? It's currently 240 commits behind.

@adamalston adamalston left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix looks good overall. I'd like to see the additional test coverage before approving.

Co-authored-by: Adam Alston <aalston9@gmail.com>

This branch was successfully deployed

1 active deployment
@carbon/react storybook preview — 229ff18a Deployed Nov 16, 2026 by benjamin-t-santos via react-storybook-preview #45301
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: DataTable: headers and rows update out of sync across renders when props change

5 participants