-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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] Possible bug in useGridApiRef
after upgrade to v7.23.6
#16135
Comments
Hello, The problem is the hook Waiting some update from MUI before to go back to the latest version. |
useGridApiRef
after upgrade to v7.23.6
Hi @doberkofler The new behavior is actually the correct one. Still, the team has decided that we will revert this back for v7 and mention this as a breaking change in the migration notes for v8 because it can create a lot of burden for the current users of v7. @GiuntaLucas we will also make sure that |
Hi @arminmeh I personally have no problem with breaking changes as long as they are documented and I understand what needs to be adapted but understand why the team decided to revert this change. Did I understand correctly that the code would need to make sure current is not null every time the reference is used? const apiRef = useGridApiRef();
const row = apiRef.current ? apiRef.current.getRow<Record<string, unknown>>(params.id) : null; Thank you! |
Yes, you would have to check that for Correct me if I am wrong @arminmeh, but we still need to check for So, the main difference would be that we can drop the second check: -apiRef.current?.getRow?.()
+apiRef.current?.getRow() @doberkofler in your case you could also shorten the codeblock to this: const apiRef = useGridApiRef();
const row = apiRef.current?.getRow<Record<string, unknown>>?.(rowId) || null; |
The documentation is also incorrect with this type change: https://mui.com/x/react-data-grid/row-grouping/#hide-the-grouped-columns I'm trying to do this for example and getting a type error: const apiRef = useGridApiRef()
const initialState = useKeepGroupedColumnsHidden({
apiRef,
initialState: {
columns: {
columnVisibilityModel: {
dueDate: false,
actioned: false,
pharmacy: false,
},
},
},
}) |
@michelengelen |
Just to update everyone Fix for the type mismatch is merged and should be released by the end of the week. As mentioned before, I am working on having this reverted back to |
Thank you @arminmeh ! |
I'm trying to understand how to safely use the API in 7.23.6 but am still a little confused. import React from "react";
import { useLayoutEffect } from "react";
import { createRoot } from "react-dom/client";
import { DataGridPro, useGridApiRef, type GridApi } from "@mui/x-data-grid-pro";
export type apiRefType = React.MutableRefObject<GridApi>;
type GridPropsType = {
readonly updateApiRef: (ref: apiRefType) => void;
};
const Grid = ({ updateApiRef }: GridPropsType) => {
const apiRef = useGridApiRef();
useLayoutEffect(() => {
if (!apiRef.current) {
throw new Error("Internal error");
}
updateApiRef(apiRef as apiRefType);
}, [updateApiRef, apiRef]);
return (
<div style={{ height: "100%", width: "100%" }}>
<DataGridPro apiRef={apiRef} columns={[]} rows={[]} />
</div>
);
};
type renderGridType = {
container: HTMLElement;
};
export const renderGrid = async (
options: renderGridType
): Promise<apiRefType> => {
return new Promise((resolve) => {
createRoot(options.container).render(<Grid updateApiRef={resolve} />);
});
}; |
@doberkofler does the upgrade to 7.24.0 solve the problem for your code snippet? |
@arminmeh Yes, with 7.24.0 my code snipped works. Does my use of |
Yes From version 8 onwards, you will have to use the check from the snippet since the ref can be |
This issue has been closed. If you have a similar problem but not exactly the same, please open a new issue. Note @doberkofler How did we do? Your experience with our support team matters to us. If you have a moment, please share your thoughts in this short Support Satisfaction survey. |
@arminmeh The problem seems to have reappeared after upgrading to 7.24.1 |
In 7.24.1 we have released my update for the types which should have resulted in this matrix. Unfortunately, one small issue remained which is causing problems for Pro and Premium packages in combination with React 18. I have already opened a PR with the fix. It should land in the next week's release. As mentioned in the comment
|
Steps to reproduce
Up until 7.23.5, the
useGridApiRef
hook return an object with thecurrent
property of typeGridApi
but with 7.23.5 the property can also be null and therefore code like the following can no longer be compiled using TypeScript.Current behavior
error TS18047: 'apiRef.current' is possibly 'null'.
Expected behavior
apiRef.current
to be of type GridApiContext
No response
Your environment
npx @mui/envinfo
Search keywords: useGridApiRef typescript current
The text was updated successfully, but these errors were encountered: