react-query#UseMutationOptions TypeScript Examples

The following examples show how to use react-query#UseMutationOptions. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: useMutation.ts    From react-starter-boilerplate with MIT License 7 votes vote down vote up
useMutation = <TData = unknown, TError = unknown, TParams = unknown, TContext = unknown>(
  mutationKey: MutationKey,
  mutation: MutationFn<TParams, TData, TError>,
  options?: UseMutationOptions<TData, TError, TParams, TContext>,
): UseMutationResult<TData, TError, TParams, TContext> => {
  const { mutationFn } = useApiClient();
  const _mutationFn = useMemo(() => mutationFn<TParams, TData>(mutation), [mutationFn, mutation]);

  return useRQMutation<TData, TError, TParams, TContext>(mutationKey, _mutationFn, options);
}
Example #2
Source File: useMutateResource.ts    From ke with MIT License 6 votes vote down vote up
export function useMutateResource<ResourceData, SourceData, TError = unknown, TContext = unknown>(
  userConfigOrKey: ResourceOptionsOrKey<MutateResourceOptions<ResourceData, SourceData, TError, TContext>>,
  requestOptions: MutationOptions<ResourceData, SourceData, TError, TContext> = {}
): MutationResult<ResourceData, SourceData, TError, TContext> {
  const userConfig = useConfigResolver(userConfigOrKey)
  const {
    mutate: { fn },
  } = useDefaultResourceConfig<ResourceData, SourceData>()
  const { key, ...options } = userConfig

  const { requestConfig, overrideGlobalOnError, mutationFn, ...mutationOptions } = deepmerge(options, requestOptions)
  const queryClient = useQueryClient()
  const globalOnError = queryClient.getDefaultOptions()?.mutations?.onError

  if (!!mutationOptions.onError && globalOnError && !overrideGlobalOnError) {
    mutationOptions.onError = injectInCallback(mutationOptions.onError, globalOnError)
  }

  const mutateFunction = mutationFn || ((data: SourceData) => fn(key, data, requestConfig))

  return useMutation<ResourceData, TError, SourceData, TContext>(
    mutateFunction,
    mutationOptions as UseMutationOptions<ResourceData, TError, SourceData, TContext>
  )
}