From 65b4be0aac35235e0ac2d06b7221e69af4091a9d Mon Sep 17 00:00:00 2001 From: Kartik Gupta Date: Mon, 4 Nov 2024 10:26:29 +0530 Subject: [PATCH] fix table going in infinite loop when using dynamic columns without id (#2527) --- frontend/src/AppBuilder/Widgets/Table/Table.jsx | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/frontend/src/AppBuilder/Widgets/Table/Table.jsx b/frontend/src/AppBuilder/Widgets/Table/Table.jsx index 559ca42267..8bbe06ffd6 100644 --- a/frontend/src/AppBuilder/Widgets/Table/Table.jsx +++ b/frontend/src/AppBuilder/Widgets/Table/Table.jsx @@ -579,7 +579,22 @@ export const Table = React.memo( properties.autogenerateColumns ?? false, id ); - useDynamicColumn && setGeneratedColumn(generatedColumnFromData); + if (useDynamicColumn) { + const dynamicColumnHasId = dynamicColumn && dynamicColumn.every((column) => 'id' in column); + if (!dynamicColumnHasId) { + // if dynamic columns do not have an id then we need to manually compare the generated columns with the columns in the state because the id that we generate for columns without id is a uuid and it will be different every time + const generatedColumnsWithoutIds = generatedColumnFromData.map(({ id, ...rest }) => ({ + ...rest, + })); + const columnsFromStateWithoutIds = generatedColumn.map(({ id, ...rest }) => ({ + ...rest, + })); + !isEqual(generatedColumnsWithoutIds, columnsFromStateWithoutIds) && + setGeneratedColumn(generatedColumnFromData); + return; + } + setGeneratedColumn(generatedColumnFromData); + } } }, [JSON.stringify(tableData), JSON.stringify(dynamicColumn)]);