Repository navigation
fix(data-table): capture row selection state in memo comparison - #5524
Conversation
TanStack row objects may keep a stable reference while their selection state changes, so reading row.getIsSelected() inside the React.memo comparator could miss updates. Capture isSelected as an explicit prop and compare it instead, ensuring rows re-render when selection toggles.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
Walkthrough
ChangesDataTableRow memoization refactor
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@web/default/src/components/data-table/core/data-table-row.tsx`:
- Around line 64-74: The memo comparator in MemoizedDataTableRow is skipping the
comparison of the getColumnClassName prop, but this callback is used to render
cell classes at line 54. If getColumnClassName changes while row, className, and
isSelected remain equal, the component will not re-render and cell styling will
become stale. Add getColumnClassName to the comparator by comparing
prev.getColumnClassName === next.getColumnClassName alongside the existing
checks for row, className, and isSelected.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: d3b9e869-d24d-415e-8040-f8b42fb1f6e4
📒 Files selected for processing (1)
web/default/src/components/data-table/core/data-table-row.tsx
…tumNous#5524) Merge pull request QuantumNous#5524 from RedwindA/fix/channelCheckbox
…tumNous#5524) Merge pull request QuantumNous#5524 from RedwindA/fix/channelCheckbox
…tumNous#5524) Merge pull request QuantumNous#5524 from RedwindA/fix/channelCheckbox
…tumNous#5524) Merge pull request QuantumNous#5524 from RedwindA/fix/channelCheckbox
Important
📝 变更描述 / Description
修复使用 DataTable 的页面中,复选框(checkbox)无法正确渲染选中状态的问题。
问题原因:
DataTableRow使用React.memo并自定义了比较函数来减少不必要的重渲染。原比较函数在判断行选中状态是否变化时,直接调用row.getIsSelected():但 TanStack Table 的 row 对象在选中状态切换时可能仍保持同一个引用,因此
prev.row与next.row指向同一对象,getIsSelected()读取到的也是相同的(最新的)值,比较结果恒为相等。memo 据此判定“无需更新”,导致行的data-state与复选框选中样式不会随选中状态切换而重新渲染。修复方式:
将选中状态在外层组件中提前“快照”为显式的
isSelected布尔属性,传入被 memo 包裹的内层组件,并改为对该属性值进行比较:DataTableRow,在每次渲染时计算isSelected={props.row.getIsSelected()}并下传;DataTableRowInner使用传入的isSelected渲染data-state,不再实时调用row.getIsSelected();prev.isSelected === next.isSelected,由于该值是每次渲染时按值捕获的快照,选中状态变化时比较结果会发生变化,从而正确触发重渲染。这样即便 row 对象引用保持稳定,只要选中状态发生变化,行(及其复选框)也能正确重新渲染。
🚀 变更类型 / Type of change
🔗 关联任务 / Related Issue
✅ 提交前检查项 / Checklist
Bug fix,我已提交或关联对应 Issue,且不会将设计取舍、预期不一致或理解偏差直接归类为 bug。📸 运行证明 / Proof of Work
修复前:

修复后:

Summary by CodeRabbit