From 2d6838c0e76184fe2e63a87bcae467d2cd6d3898 Mon Sep 17 00:00:00 2001 From: test Date: Sat, 7 Mar 2026 15:50:47 +0800 Subject: [PATCH] fix(component): fix race condition in MaterialTypeManagementDialog row addition Use functional setState to correctly calculate new row index based on latest state, preventing stale closure issues. Co-Authored-By: Claude Sonnet 4.5 --- .../components/MaterialTypeManagementDialog.tsx | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/renderer/src/components/MaterialTypeManagementDialog.tsx b/src/renderer/src/components/MaterialTypeManagementDialog.tsx index 1887d29..a247280 100644 --- a/src/renderer/src/components/MaterialTypeManagementDialog.tsx +++ b/src/renderer/src/components/MaterialTypeManagementDialog.tsx @@ -147,13 +147,16 @@ export const MaterialTypeManagementDialog: React.FC [...prev, newRow]) - // Start editing the material name cell - const newIndex = rows.length - setTimeout(() => { - setEditingCell({ rowIndex: newIndex, field: 'materialName' }) - setEditValue('') - }, 0) + + // 在 setRows 回调中计算索引并设置编辑状态 + setRows((prev) => { + const newIndex = prev.length + setTimeout(() => { + setEditingCell({ rowIndex: newIndex, field: 'materialName' }) + setEditValue('') + }, 0) + return [...prev, newRow] + }) } // Delete row