Skip to content
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

Merged
merged 1 commit into from
Feb 9, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions packages/grid/_modules_/grid/hooks/features/useApiRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ function createGridApi(): GridApi {
return new EventEmitter() as GridApi;
}

export function useApiRef(apiRefProp?: ApiRef): ApiRef {
const apiRef = React.useRef<GridApi>(createGridApi());
// Public developers facing overload
export function useApiRef(): ApiRef;

React.useEffect(() => {
if (apiRefProp?.current) {
apiRefProp.current = apiRef.current;
}
});
// Internal grid facing overload
export function useApiRef(apiRefProp: ApiRef | undefined): ApiRef;
Copy link
Member

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

Copy link
Member Author

@oliviertassinari oliviertassinari Feb 8, 2021

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.


export function useApiRef(...args): any {
const apiRefProp = args[0];
const apiRef = React.useRef<GridApi>(args.length === 0 ? null : createGridApi());

React.useImperativeHandle(apiRefProp, () => apiRef.current, [apiRef]);

return apiRef;
}