diff --git a/packages/grid/_modules_/grid/hooks/features/useApiRef.ts b/packages/grid/_modules_/grid/hooks/features/useApiRef.ts index 5765d3e5e063d..25584cf1b8805 100644 --- a/packages/grid/_modules_/grid/hooks/features/useApiRef.ts +++ b/packages/grid/_modules_/grid/hooks/features/useApiRef.ts @@ -10,14 +10,17 @@ function createGridApi(): GridApi { return new EventEmitter() as GridApi; } -export function useApiRef(apiRefProp?: ApiRef): ApiRef { - const apiRef = React.useRef(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; + +export function useApiRef(...args): any { + const apiRefProp = args[0]; + const apiRef = React.useRef(args.length === 0 ? null : createGridApi()); + + React.useImperativeHandle(apiRefProp, () => apiRef.current, [apiRef]); return apiRef; }