@supunlakmal/hooks
Version:
A collection of reusable React hooks
44 lines (43 loc) • 2.14 kB
TypeScript
type MutationStatus = 'idle' | 'loading' | 'success' | 'error';
interface UseMutationResult {
/** The current status of the mutation. */
status: MutationStatus;
/** The data returned from a successful mutation. */
data: any;
/** The error object if the mutation failed. */
error: any;
/** Boolean indicating if the mutation is currently executing. */
isLoading: boolean;
/** Boolean indicating if the mutation has successfully completed. */
isSuccess: boolean;
/** Boolean indicating if the mutation failed. */
isError: boolean;
/** Boolean indicating if the mutation has not started yet. */
isIdle: boolean;
/** The function to trigger the mutation. Accepts variables if the mutation function requires them. */
mutate: (variables: any) => Promise<any>;
/** Async version of mutate */
mutateAsync: (variables: any) => Promise<any>;
/** Resets the mutation state back to idle. */
reset: () => void;
}
interface UseMutationOptions {
/** Callback function executed on successful mutation. Receives the data and variables. */
onSuccess?: (data: any, variables: any) => void;
/** Callback function executed on mutation error. Receives the error and variables. */
onError?: (error: any, variables: any) => void;
/** Callback function executed when the mutation starts. Receives the variables. */
onMutate?: (variables: any) => void;
/** Callback function executed when mutation finishes (either success or error). Receives data/error and variables. */
onSettled?: (data: any, error: any, variables: any) => void;
}
/**
* Hook to manage asynchronous operations that modify data (mutations).
* Handles loading, success, and error states, and provides callbacks.
*
* @param mutationFn The asynchronous function that performs the mutation.
* @param options Optional configuration with callbacks (onSuccess, onError, etc.).
* @returns State and functions to manage the mutation lifecycle.
*/
export declare const useMutation: (mutationFn: (variables: any) => Promise<any>, options?: UseMutationOptions) => UseMutationResult;
export {};