ra-core
Version:
Core components of react-admin, a frontend Framework for building admin applications on top of REST services, using ES6, React
27 lines (24 loc) • 898 B
text/typescript
import { useEffect } from 'react';
import { Middleware } from './useMutationMiddlewares';
import { useSaveContext } from './useSaveContext';
/**
* Internal hook that registers a middleware for the save function in the current SaveContext.
* @param callback The middleware function.
*/
export const useRegisterMutationMiddleware = <
MutateFunc extends (...args: any[]) => any = (...args: any[]) => any,
>(
callback: Middleware<MutateFunc>
) => {
const { registerMutationMiddleware, unregisterMutationMiddleware } =
useSaveContext();
useEffect(() => {
if (!registerMutationMiddleware || !unregisterMutationMiddleware) {
return;
}
registerMutationMiddleware(callback);
return () => {
unregisterMutationMiddleware(callback);
};
}, [callback, registerMutationMiddleware, unregisterMutationMiddleware]);
};