Skip to content

useMutation ​

Hook that defines the logic when mutate data

utilities
test coverage
Last changed: last month

Installation ​

Library
CLI
Manual
typescript
import { useMutation } from '@siberiacancode/reactuse';

Usage ​

typescript
const { mutate, mutateAsync, isLoading, isError, isSuccess, error, data } = useMutation((name) => Promise.resolve(name));

Demo ​

Api ​

Parameters

NameTypeDefaultNote
callback(body: Body) => Promise<Data>-The callback function to be invoked
options.retry?boolean | number-The retry count of requests
options.onSuccess?(data: Data) => void-The callback function to be invoked on success
options.onError?(error: Error) => void-The callback function to be invoked on error

Returns

UseMutationReturn<Data>

Type declaration ​

typescript
interface UseMutationOptions<Data> {
  /* The retry count of requests */
  retry?: ((failureCount: number, error: Error) => boolean) | boolean | number;
  /* The retry delay of requests */
  retryDelay?: ((retry: number, error: Error) => number) | number;
  /* The callback function to be invoked on error */
  onError?: (error: Error) => void;
  /* The callback function to be invoked on success */
  onSuccess?: (data: Data) => void;
}

interface UseMutationReturn<Body, Data> {
  /* The data of the mutation */
  data: Data | null;
  /* The error of the mutation */
  error: Error | null;
  /* The error state of the mutation */
  isError: boolean;
  /* The loading state of the mutation */
  isLoading: boolean;
  /* The success state of the mutation */
  isSuccess: boolean;
  /* The mutate function */
  mutate: (body?: Body, options?: UseMutationOptions<Data>) => void;
  /* The mutate async function */
  mutateAsync: (body?: Body, options?: UseMutationOptions<Data>) => Promise<Data>;
}

export interface RequestOptions<Data> extends UseMutationOptions<Data> {
  /* The attempt count */
  attempt?: number;
}

Source ​

Source • Demo

Contributors ​

D
debabin
debabin
H
hywax
hywax

Released under the MIT License.