@mui/x-data-grid
Version:
The community edition of the data grid component (MUI X).
25 lines (24 loc) • 823 B
JavaScript
import * as React from 'react';
export function useGridApiMethod(apiRef, apiMethods, // TODO: Remove `apiName
// eslint-disable-next-line @typescript-eslint/no-unused-vars
apiName) {
const apiMethodsRef = React.useRef(apiMethods);
const [apiMethodsNames] = React.useState(Object.keys(apiMethods));
const installMethods = React.useCallback(() => {
if (!apiRef.current) {
return;
}
apiMethodsNames.forEach(methodName => {
if (!apiRef.current.hasOwnProperty(methodName)) {
apiRef.current[methodName] = (...args) => apiMethodsRef.current[methodName](...args);
}
});
}, [apiMethodsNames, apiRef]);
React.useEffect(() => {
apiMethodsRef.current = apiMethods;
}, [apiMethods]);
React.useEffect(() => {
installMethods();
}, [installMethods]);
installMethods();
}