-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[data grid] Re-mounting DataGrid with apiRef causes error (apiRef.current is null) #6879
Comments
We don't set explicitly
We can add diff --git a/packages/grid/x-data-grid-pro/src/models/dataGridProProps.ts b/packages/grid/x-data-grid-pro/src/models/dataGridProProps.ts
index c4a383678..669a9e3c3 100644
--- a/packages/grid/x-data-grid-pro/src/models/dataGridProProps.ts
+++ b/packages/grid/x-data-grid-pro/src/models/dataGridProProps.ts
@@ -132,7 +132,7 @@ export interface DataGridProPropsWithoutDefaultValue<R extends GridValidRowModel
/**
* The ref object that allows grid manipulation. Can be instantiated with [[useGridApiRef()]].
*/
- apiRef?: React.MutableRefObject<GridApiPro>;
+ apiRef?: React.MutableRefObject<GridApiPro | null>;
/**
* The initial state of the DataGridPro.
* The data in it will be set in the state on initialization but will not be controlled.
diff --git a/packages/grid/x-data-grid/src/models/props/DataGridProps.ts b/packages/grid/x-data-grid/src/models/props/DataGridProps.ts
index 79744a8ee..1ac29cd88 100644
--- a/packages/grid/x-data-grid/src/models/props/DataGridProps.ts
+++ b/packages/grid/x-data-grid/src/models/props/DataGridProps.ts
@@ -350,7 +350,7 @@ export interface DataGridPropsWithoutDefaultValue<R extends GridValidRowModel =
* TODO: Remove `@internal` when opening `apiRef` to Community plan
* @ignore - do not document.
*/
- apiRef?: React.MutableRefObject<GridApiCommunity>;
+ apiRef?: React.MutableRefObject<GridApiCommunity | null>;
/**
* Signal to the underlying logic what version of the public component API
* of the data grid is exposed [[GridSignature]]. Then we'll have to update a bunch of other files to also accept As a workaround for now, you can move |
Is this likely to get fixed please? Unfortunately we can't move our |
I fall into the same problem. Rendering conditionally might solve partially the problem but leads to others. Look this construction, we have 2 components and we switch the view among them. We do not want to dismount them, otherwise we lose the state on all the components inside, thus we use "freeze".
Switching from Foo to Bar works, when we return to Foo the warning is shown. |
Is there a plan to resolve this? seems like an open issue for a while. I'm having the same problem with the pro version. |
This also happens when the grid is Suspended. https://codesandbox.io/p/sandbox/datagrid-null-ref-on-suspense-72nnlt (edit: wrong link, updated) Note: in the sandbox this only produces a warning but in our production deploy GridRow still renders even though the component is suspended (not sure how. working on a tighter repro) and causes null reference errors! Is there any recommendation around using DataGrid with suspense? |
From what I read in the top comment, this isn't throwing an error, it's displaying a warning. If it's the case, the workaround listed there (setting the ref value to an empty object) is also the best way to deal with this issue. This is a prop-types runtime warning that is safe to ignore, and they should never be enabled on a production build. I don't think that widening the typings to accept @scamden If there is a runtime error, provide the reproduction case and we'll take a look. |
@romgrk I would agree accept that the use of useImperativeHandle means that I do have a runtime error. I'm having a hard time creating a repro in codesandbox but basically GridRow renders while a parent is suspending (and therefore current is null). Take a look at the code sandbox I provided which shows that the ref becomes null during suspension (which seems a lot more dangerous than during unmount and is the source of the run time error I haven't easily reproduced) I have also seen |
If you need clean conditional rendering, you can wrap the The runtime errors are really worrying though, if you're able to get us a reproduction it would be greatly appreciated. The logic is supposed to initialized |
Here i've finally got a repro for this: click Suspend button and witness the errror: |
I think we can avoid the issue by avoiding @mui/xgrid Any thoughts on dropping |
This sounds good to me! |
I've thought about it some more, and not setting it to |
I created a minimal reproduction example using the v6: https://stackblitz.com/edit/react-1fe1ex?file=Demo.tsx I believe the issue was fixed by #11792 @scamden Can you give v7 a try? |
The issue has been inactive for 7 days and has been automatically closed. |
Did that PR fix the issue completely? |
The issue has been inactive for 7 days and has been automatically closed. |
Is there a good workaround for this? |
Duplicates
Latest version
Steps to reproduce 🕹
CodeSandbox: https://codesandbox.io/s/amazing-panini-4pyuru?file=/demo.tsx
I'm trying to use
useGridApiRef
with a<DataGridPro>
that gets mounted conditionally. Here's the simplest example I could come up with:Current behavior 😯
After mounting, un-mounting and re-mounting the component, the following error is raised:
The fact that
apiRef.current
can benull
also means that additional checks are necessary when accessing it, e.g. in an effect hook. This is not reflected by the corresponding typeReact.MutableRefObject<GridApiPro>
.Adding the following workaround solves the runtime issues:
Expected behavior 🤔
Don't throw an error for the above code – it doesn't do anything crazy.
To solve this, I assume one would have to
either:
Don't set
apiRef.current
tonull
– Note that setting an empty object, as theuseGridApiRef
hook does on init, might also cause issues as it does not match theDataGridPro
type.or:
Make
null
a legal value forapiRef.current
and also reflect that in the typing ofuseGridApiRef
.Context 🔦
We retrieve data from our backend to render it in a table. Said request may fail, in which case we show an error message in place of the table.
The
apiRef
is used to track density changes so we can persist them in localStorage.Here's a rough sketch of what our code does:
Your environment 🌎
Order ID 💳 (optional)
We have one but I have to check if I'm allowed to post it.
The text was updated successfully, but these errors were encountered: