Giter Site home page Giter Site logo

When i try to give the chekcboxSelectionn a default value, React is throwing the error: Cannot update a component (`ControlledSelectionGrid`) while rendering a different component (`ForwardRef(DataGrid)`). To locate the bad setState()... about mui-x HOT 2 OPEN

john1625b avatar john1625b commented on June 6, 2024
When i try to give the chekcboxSelectionn a default value, React is throwing the error: Cannot update a component (`ControlledSelectionGrid`) while rendering a different component (`ForwardRef(DataGrid)`). To locate the bad setState()...

from mui-x.

Comments (2)

m4theushw avatar m4theushw commented on June 6, 2024 1

This error occurs because we have a special logic to remove, from the specified selection model, those rows that are not in the rows prop. This logic, in the first render, is triggered during the render phrase because it listens to the sortedRowsSet event.

const removeOutdatedSelection = React.useCallback(() => {
if (props.keepNonExistentRowsSelected) {
return;
}
const currentSelection = gridRowSelectionStateSelector(apiRef.current.state);
const rowsLookup = gridRowsLookupSelector(apiRef);
// We clone the existing object to avoid mutating the same object returned by the selector to others part of the project
const selectionLookup = { ...selectedIdsLookupSelector(apiRef) };
let hasChanged = false;
currentSelection.forEach((id: GridRowId) => {
if (!rowsLookup[id]) {
delete selectionLookup[id];
hasChanged = true;
}
});
if (hasChanged) {
apiRef.current.setRowSelectionModel(Object.values(selectionLookup));
}
}, [apiRef, props.keepNonExistentRowsSelected]);

I tried to fix below this error by not updating the state during the render phase. It's not very elegant because it relays on an effect.

diff --git a/packages/grid/x-data-grid/src/hooks/features/rowSelection/useGridRowSelection.ts b/packages/grid/x-data-grid/src/hooks/features/rowSelection/useGridRowSelection.ts
index baa86964e..5babb09d3 100644
--- a/packages/grid/x-data-grid/src/hooks/features/rowSelection/useGridRowSelection.ts
+++ b/packages/grid/x-data-grid/src/hooks/features/rowSelection/useGridRowSelection.ts
@@ -526,11 +526,17 @@ export const useGridRowSelection = (
     [apiRef, handleSingleRowSelection, selectRows, visibleRows.rows, canHaveMultipleSelection],
   );
 
-  useGridApiEventHandler(
-    apiRef,
-    'sortedRowsSet',
-    runIfRowSelectionIsEnabled(removeOutdatedSelection),
-  );
+  const [sortedRowsSet, setSortedRowsSet] = React.useState([]);
+
+  const handleSortedRowsSet = React.useCallback((sortedRows) => {
+    setSortedRowsSet(sortedRows);
+  }, []);
+
+  React.useEffect(() => {
+    removeOutdatedSelection();
+  }, [sortedRowsSet, removeOutdatedSelection]);
+
+  useGridApiEventHandler(apiRef, 'sortedRowsSet', runIfRowSelectionIsEnabled(handleSortedRowsSet));
   useGridApiEventHandler(apiRef, 'rowClick', runIfRowSelectionIsEnabled(handleRowClick));
   useGridApiEventHandler(
     apiRef,
diff --git a/packages/grid/x-data-grid/src/hooks/features/sorting/useGridSorting.ts b/packages/grid/x-data-grid/src/hooks/features/sorting/useGridSorting.ts
index a5847ddb7..f55a4c1ef 100644
--- a/packages/grid/x-data-grid/src/hooks/features/sorting/useGridSorting.ts
+++ b/packages/grid/x-data-grid/src/hooks/features/sorting/useGridSorting.ts
@@ -166,7 +166,7 @@ export const useGridSorting = (
       };
     });
 
-    apiRef.current.publishEvent('sortedRowsSet');
+    apiRef.current.publishEvent('sortedRowsSet', apiRef.current.state.sorting.sortedRows);
     apiRef.current.forceUpdate();
   }, [apiRef, logger, props.sortingMode]);

A workaround for now is to enable the keepNonExistentRowsSelected prop. The drawback is that the selected row count in the footer will include non-existent rows.

from mui-x.

john1625b avatar john1625b commented on June 6, 2024

ok keepNonExistentRowsSelected works for me as an interim solution. I don't think I'll close the issue since there's still work to be done.

from mui-x.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.