Skip to main content
Version: v8.0.0

useSubmit

This hook aims to mutate data on the server. Unlike useFetch, it does not fire automatically - call the returned submit function to trigger the request (e.g., on button click).


Import
import { useSubmit } from "@hyper-fetch/react"

Parameters​

useSubmit<RequestType, TSelected>(request: RequestType, options: UseSubmitOptionsType<RequestType, TSelected>)
Parameters
NameTypeDescription
request
RequestType
options
UseSubmitOptionsType<RequestType, TSelected>

Returns​

type UseSubmitReturnType<RequestType,TSelected> = Omit<UseTrackedStateType<RequestType, TSelected>, loading> & UseTrackedStateActions<RequestType, TSelected> & { abort: () => void; bounce: { active: boolean; reset: () => void }; onSubmitAbort: (callback: OnErrorCallbackType<RequestType>) => void; onSubmitDownloadProgress: (callback: OnProgressCallbackType) => void; onSubmitError: (callback: OnErrorCallbackType<RequestType>) => void; onSubmitFinished: (callback: OnFinishedCallbackType<RequestType>) => void; onSubmitOfflineError: (callback: OnErrorCallbackType<RequestType>) => void; onSubmitRequestStart: (callback: OnStartCallbackType<RequestType>) => void; onSubmitResponseStart: (callback: OnStartCallbackType<RequestType>) => void; onSubmitSuccess: (callback: OnSuccessCallbackType<RequestType>) => void; onSubmitUploadProgress: (callback: OnProgressCallbackType) => void; refetch: () => void; submit: RequestSendType<RequestType>; submitting: boolean };
useSubmit
NameTypeDescription
bounce
{ active: boolean; reset: () => void }

Data related to current state of the bounce usage

submitting
boolean

Request loading state

clearState
() => void

Reset all state fields to their initial values (data, error, status, extra → null, loading → false, etc.).

setData
(data: CacheSetState<TData | null>) => void

Action to set custom data. We can do it locally(inside hook state). When select is used it receives the selected data shape. If you need to update cache data use client.cache.update(). method.

setError
(error: CacheSetState<ExtractErrorType<T> | null>) => void

Action to set custom error. We can do it locally(inside hook state). If you need to update cache data use client.cache.update() method.

setExtra
(extra: CacheSetState<ExtractAdapterExtraType<ExtractAdapterType<T>> | null>) => void

Action to set custom additional data. We can do it locally(inside hook state). If you need to update cache data use client.cache.update() method.

setLoading
(loading: CacheSetState<boolean>) => void

Action to set custom loading. We can do it locally(inside hook state). If you need to update cache data use client.cache.update() method.

setRequestTimestamp
(timestamp: CacheSetState<Date>) => void

Action to set custom timestamp. We can do it locally(inside hook state). If you need to update cache data use client.cache.update() method.

setResponseTimestamp
(timestamp: CacheSetState<Date>) => void

Action to set custom timestamp. We can do it locally(inside hook state). If you need to update cache data use client.cache.update() method.

setRetries
(retries: CacheSetState<number>) => void

Action to set custom retries count. We can do it locally(inside hook state). If you need to update cache data use client.cache.update() method.

setStatus
(status: CacheSetState<ExtractAdapterStatusType<ExtractAdapterType<T>>>) => void

Action to set custom status. We can do it locally(inside hook state). If you need to turn on loading for all listening hooks use client.requestManager.events.emitLoading() method.

setSuccess
(success: CacheSetState<boolean>) => void

Action to set custom success. We can do it locally(inside hook state). If you need to update cache data use client.cache.update() method.

abort
() => void

Callback which allows to cancel ongoing requests from given queryKey.

onSubmitAbort
(callback: OnErrorCallbackType<RequestType>) => void

Helper hook listening on aborting of requests. Includes mutationContext when setOptimistic is configured. Abort events are not triggering onError callbacks.

onSubmitDownloadProgress
(callback: OnProgressCallbackType) => void

Helper hook listening on download progress ETA. We can later match given requests by their id's or request instance which holds all data which is being transferred.

onSubmitError
(callback: OnErrorCallbackType<RequestType>) => void

Helper hook listening on error response. Includes mutationContext when setOptimistic is configured.

onSubmitFinished
(callback: OnFinishedCallbackType<RequestType>) => void

Helper hook listening on any response. Includes mutationContext when setOptimistic is configured.

onSubmitOfflineError
(callback: OnErrorCallbackType<RequestType>) => void

Helper hook listening on request going into offline awaiting for network connection to be restored. It will not trigger onError when 'offline' mode is set on request.

onSubmitRequestStart
(callback: OnStartCallbackType<RequestType>) => void

Helper hook listening on request start.

onSubmitResponseStart
(callback: OnStartCallbackType<RequestType>) => void

Helper hook listening on response start(before we receive all data from server).

onSubmitSuccess
(callback: OnSuccessCallbackType<RequestType>) => void

Helper hook listening on success response. Includes mutationContext when setOptimistic is configured.

onSubmitUploadProgress
(callback: OnProgressCallbackType) => void

Helper hook listening on upload progress ETA. We can later match given requests by their id's or request instance which holds all data which is being transferred.

refetch
() => void

Refetch current request

submit
RequestSendType<RequestType>

Method responsible for triggering requests. It return Promise which will be resolved with the request.