-
-
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
[DataGrid] Resolve the api ref at the same time as any other ref #990
Conversation
} | ||
}); | ||
// Internal grid facing overload | ||
export function useApiRef(apiRefProp: ApiRef | undefined): ApiRef; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand why we need an overload here, instead of an optional arg
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are two different and distinct use cases: internal, and developer-facing. Ideally, I think that we should create two different modules or rely on a simple ref developer-facing.
You're losing the type, it's better to use |
@dtassone I was hoping TypeScript could infer the correct type but I didn't test it. I work fine with HTML refs. So I don't see why it wouldn't in our case. I believe the current types are also lying: https://codesandbox.io/s/material-demo-forked-256vk?file=/demo.tsx. We can even do: import * as React from "react";
import { XGrid, useApiRef } from "@material-ui/x-grid";
import { useDemoData } from "@material-ui/x-grid-data-generator";
export default function ApiRefPaginationGrid() {
const apiRef = useApiRef();
const { data } = useDemoData({
dataSet: "Commodity",
rowLength: 10,
maxColumns: 6
});
apiRef.current.setPage(2);
return (
<div style={{ height: 400, width: "100%" }}>
<XGrid pagination pageSize={5} apiRef={apiRef} {...data} />
</div>
);
} without TypeScript complaining. |
Another benefit of the approach, it allows the usage of the apiRef with class components: https://codesandbox.io/s/material-demo-forked-wvq55?file=/demo.tsx |
Well that worked with the previous approach as well. |
@dtassone It crashes on my end: |
make sure you use the latest version of xgrid |
@dtassone Oh wow, so strange, same URL, different behavior for both of us 😆. @DanailH How does it behave in your env? In any case, it's not that important. The motivation for the changes is to:
|
Effectively, it means that we can stop documenting
useApiRef
, and replace it completely in the public API with:It's a logical follow-up with the direction taken in #933. More details in #939 (comment). I like the approach, at least, it's sounds and consistent with the rest of the codebase. You can find the same API on, the Tabs for instance (
action
prop).