Skip to content

refactor: improve tbody key computation in DataTable#1380

Open
vinitkhandal717 wants to merge 2 commits intodevfrom
1375-datatable-data-table-doesnt-update-on-search-probably-caches-rows
Open

refactor: improve tbody key computation in DataTable#1380
vinitkhandal717 wants to merge 2 commits intodevfrom
1375-datatable-data-table-doesnt-update-on-search-probably-caches-rows

Conversation

@vinitkhandal717
Copy link
Copy Markdown
Collaborator

@vinitkhandal717 vinitkhandal717 commented May 1, 2026

Summary

Refactor key/remount logic in TableBody

Issue Ticket

Closes #1375

Screen.Recording.2026-05-01.at.2.00.17.PM.mov

Uploading Screen Recording 2026-05-01 at 2.01.09 PM.mov…

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented May 1, 2026

Open in StackBlitz

npm i https://pkg.pr.new/@juspay/blend-design-system@1380

commit: a001ade

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates the DataTable body remount key to better reflect changes in the displayed rows, addressing the reported issue where search/filter updates could render stale/irrelevant rows unless the component was force-remounted (Closes #1375).

Changes:

  • Replaced the previous tbody key (based on length + first/last row IDs) with a computed hash derived from all row IDs.
  • Memoized the computed tbody key to avoid recomputation on unrelated renders.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +667 to +669
const tbodyKey = useMemo(() => {
return `tbody-${computeDataHash()}`
}, [currentData, idField])
const id = String(currentData[i][idField])
for (let j = 0; j < id.length; j++) {
hash = (hash << 5) - hash + id.charCodeAt(j)
hash = hash & hash // Convert to 32-bit integer
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 4 comments.

Comment on lines +640 to +668
// Compute a stable hash of the current data for React key
// This ensures proper remounting when data changes (search, filter, sort, pagination)
// Hash includes: data length + first ID + last ID + combined hash of all IDs
const computeDataHash = (): string => {
const len = currentData.length
if (len === 0) return 'empty'

// Get first and last IDs for quick comparison
const firstId = String(currentData[0][idField])
const lastId = String(currentData[len - 1][idField])

// Create a simple hash of all IDs for uniqueness
let hash = 0
for (let i = 0; i < len; i++) {
const id = String(currentData[i][idField])
for (let j = 0; j < id.length; j++) {
hash = (hash << 5) - hash + id.charCodeAt(j)
hash = hash & hash // Convert to 32-bit integer
}
}

// Convert negative hash to positive hex string
const hashHex = (hash >>> 0).toString(16)

return `${len}-${firstId}-${lastId}-${hashHex}`
}

const tbodyKey = useMemo(() => {
return `tbody-${computeDataHash()}`
Comment on lines +640 to +665
// Compute a stable hash of the current data for React key
// This ensures proper remounting when data changes (search, filter, sort, pagination)
// Hash includes: data length + first ID + last ID + combined hash of all IDs
const computeDataHash = (): string => {
const len = currentData.length
if (len === 0) return 'empty'

// Get first and last IDs for quick comparison
const firstId = String(currentData[0][idField])
const lastId = String(currentData[len - 1][idField])

// Create a simple hash of all IDs for uniqueness
let hash = 0
for (let i = 0; i < len; i++) {
const id = String(currentData[i][idField])
for (let j = 0; j < id.length; j++) {
hash = (hash << 5) - hash + id.charCodeAt(j)
hash = hash & hash // Convert to 32-bit integer
}
}

// Convert negative hash to positive hex string
const hashHex = (hash >>> 0).toString(16)

return `${len}-${firstId}-${lastId}-${hashHex}`
}
Comment on lines +667 to +669
const tbodyKey = useMemo(() => {
return `tbody-${computeDataHash()}`
}, [currentData, idField])
Comment on lines +640 to +642
// Compute a stable hash of the current data for React key
// This ensures proper remounting when data changes (search, filter, sort, pagination)
// Hash includes: data length + first ID + last ID + combined hash of all IDs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[DataTable]: Data table doesn't update on search, probably caches rows

2 participants