Skip to main content
Version: v3

Core useHooks

useRequest

Indicates the sending of a request. When useRequest is executed, a request is sent by default, and stateful request-related data, such as loading/data/error, is created and maintained. It is the most commonly used method when the page obtains initial data. It also supports turning off its default request sending, which is very useful in request scenarios triggered by click events such as submitting data.

Go to useRequest for details.

Type

function useRequest<AG extends AlovaGenerics>(
methodHandler: Method | (...args: any[]) => Method<AG>,
config?: RequestHookConfig
): UseHookUseHookExposure<AG>;

Parameters

  1. methodHandler: Can be passed in two forms: method instance and function. When specified as a function, it can receive the parameters passed in by send and require a method instance to be returned.
  2. config: Configuration parameters of hook.
NameDescriptionTypeDefaultVersion
immediateWhether to initiate a request immediatelybooleantrue-
initialDataInitial data value, before the first response, data value is the initial value, if not set, it is undefinedany--
forceWhether to force the request, can be set to a function to dynamically return a boolean valueboolean | (...args: any[]) => boolean | false--
managedStatesAdditional supervision states, can be updated through updateStateRecord<string |number | symbol, any>--
middlewareMiddleware function, Learn about alova middleware(context: AlovaFrontMiddlewareContext, next: AlovaGuardNext) => Promise<any>--

AlovaFrontMiddlewareContext

NameDescriptionTypeVersion
methodThe method object of the current requestMethod-
cachedResponseThe cache data hitany-
configThe current use hook configurationRecord<string, any>-
argsThe parameters of the response processing callback, which are passed in by the send of use hooksany[]-
proxyStatesThe proxy collection of use hook states, such as data, loading, error, etc.FrameworkStateMap-
sendSend request function(...args: any[]) => voidPromise
abortInterrupt function() => void-
controlLoadingCustomize the state of loading control, and the call will no longer trigger the change of loading state. When the control passed in is false, the control will be cancelled(control?: boolean) => void-

AlovaGuardNext

type AlovaGuardNext = (guardNextConfig?: {
force?: boolean | (...args: any[]) => boolean;
method?: Method;
}): Promise;

FrameworkStateMap

The following property values ​​are FrameworkState collections. FrameworkState is a state proxy. Click here for details

NameDescriptionTypeVersion
loadingRequest loading stateboolean-
dataResponse dataany-
errorRequest error informationError | undefined-
downloadingDownload progress informationObject-
uploadingUpload progress informationObject-

AlovaSuccessEvent

NameDescriptionTypeVersion
methodMethod object of the current requestMethod-
argsParameters of the response processing callback, which are passed in by send of use hooksany[]-
dataResponse dataany-
fromCacheWhether the response data comes from the cacheboolean-

AlovaErrorEvent

NameDescriptionTypeVersion
methodThe method object of the current requestMethod-
argsThe parameters of the response processing callback, which are passed in by the send of use hooksany[]-
errorResponse error instanceError-

AlovaCompleteEvent

NameDescriptionTypeVersion
methodThe method object of the current requestMethod-
argsThe parameters of the response processing callback, which are passed in by the send of use hooksany[]-
statusResponse status, success if successful, error if failed'success' | 'error'-
dataResponse data, with value if successfulany-
fromCacheWhether the response data comes from the cache, with value if successfulboolean-
errorResponse error instance, with value when failedError-

Return value

UseHookExposure contains response data and request-related states, operation functions, and event binding functions. They will automatically infer the responsive data type of the corresponding UI framework based on statesHook, which is Ref type in vue3, ordinary value in react, and Writable type in svelte.

Responsive data

NameDescriptionTypeVersion
loadingRequest loading statusboolean-
dataResponse dataany-
errorRequest error messageError | undefined-
downloadingDownload progress informationObject-
uploadingUpload progress informationObject-
__referingObjInternal reference object, implement useHook compatible with different UI frameworksObject-

Operation function

NameDescriptionFunction parametersReturn valueVersion
sendSend request function...args: any[]--
abortInterrupt function-Promise-
updateFunction that updates the current use hook front-end state, which is more useful in reactnewFrontStates: FrontRequestState-
__proxyStateInternal function, function to get the state proxystateKey: stringFrameworkState

Event

NameDescriptionCallback parameterVersion
onSuccessRequest success event bindingevent: AlovaSuccessEvent-
onErrorRequest error event bindingevent: AlovaErrorEvent-
onCompleteRequest completion event bindingevent: AlovaCompleteEvent-

useWatcher

Watches the state and initiates a request after the state changes. It is used in some scenarios where re-requests are required as the data changes, such as paging, data filtering, and fuzzy search.

Go to State Change Request for details.

Type

function useWatcher<AG extends AlovaGenerics>(
handler: Method | (...args: any[]) => Method<AG>,
watchingStates: State[],
config?: WatcherHookConfig
): UseHookExposure<AG>;

Parameters

  1. handler: Both method instance and function can be passed in. When specified as a function, it can receive the parameters passed in by send and is required to return a method instance.

  2. config: Configuration parameters of hook.

NameDescriptionTypeDefaultVersion
immediateWhether to initiate a request immediatelybooleanfalse-
initialDataInitial data value, before the first response, data value is the initial value, if not set, it is undefinedany--
forceWhether to force the request, can be set to a function to dynamically return a boolean valueboolean | (...args: any[]) => boolean | false
managedStatesAdditional management states, can be updated through updateStateRecord<string | number | symbol, any>-
debounceRequest debounce time (milliseconds). When passing an array, you can set the debounce time separately according to the order of watchingStatesnumber | number[]-
middlewareMiddleware function, Learn about alova middleware(context: AlovaFrontMiddlewareContext, next: AlovaGuardNext) => Promise<any>--
abortLastWhether to interrupt the last unresponsive requestbooleantrue-
__referingObjInternal reference object to implement useHook compatibility with different UI frameworksObject-

Return value

UseHookExposure contains states, operation functions, and event binding functions related to response data and requests. They will automatically infer the corresponding UI according to statesHook The responsive data type of the framework is Ref type in vue3, ordinary value in react, and Writable type in svelte.

Responsive data

NameDescriptionTypeVersion
loadingRequest loading statusboolean-
dataResponse dataany-
errorRequest error messageError | undefined-
downloadingDownload progress informationObject-
uploadingUpload progress informationObject-
__proxyStateInternal function, function to get state proxystateKey: stringFrameworkState

Operation function

NameDescriptionFunction parametersReturn valueVersion
sendSend request function...args: any[]Promise-
abortInterrupt function---
updateUpdate the current use hook Function of front-end state, more useful in reactnewFrontStates: FrontRequestState-

Event

NameDescriptionCallback parameterVersion
onSuccessRequest success event bindingevent: AlovaSuccessEvent-
onErrorRequest error event bindingevent: AlovaErrorEvent-
onCompleteRequest completion event bindingevent: AlovaCompleteEvent-

useFetcher

Use useFetcher to fetch data, which is useful for preloading data and updating status across modules.

Go to Data Fetch for details.

Type

function useFetcher(config?: FetcherHookConfig): UseFetchHookExposure;

Parameters

  1. config: Configuration parameters of hook.
NameDescriptionTypeDefaultVersion
forceWhether to force the request, can be set as a function to dynamically return a boolean valueboolean(...args: any[]) => boolean | false-
middlewareMiddleware function, Learn about alova middleware(context: AlovaFetcherMiddlewareContext, next: AlovaGuardNext) => Promise<any>--
__referingObjInternal reference object, implement useHook compatible with different UI frameworksObject-

AlovaFetcherMiddlewareContext

NameDescriptionTypeVersion
methodThe method object of the current requestMethod-
cachedResponseHit cache dataany-
configCurrent use hook configurationRecord<string, any>-
srgsResponse processing callback parameters, which are passed in by useFetcher's fetchany[]-
proxyStatesUse hook preload state proxy collection, such as loading, error, etc.FetchRequestState-
fetchData preload function(method: Method, ...args: any[]) => voidPromise
abortInterrupt function() => void-
updateFunction that updates the current use hook preload state, which is more useful in react(newFrontStates: FetchRequestState) => void;-
controlLoadingAfter calling, the loading state will be customized, and the loading state change will no longer be triggered internally. The control passed in is When false, control will be canceled(control?: boolean) => void-

FetchRequestState

The following property values ​​will automatically infer the responsive data type of the corresponding UI framework based on statesHook, which is Ref type in vue3, normal value in react, and Writable type in svelte

NameDescriptionTypeVersion
loadingPreload request statusboolean-
errorRequest error messageError | undefined-
downloadingDownload progress informationObject-
uploadingUpload progress informationObject-

Return value

UseFetchHookReturnType contains request-related states, operation functions, and event binding functions, which will automatically infer the responsive data type of the corresponding UI framework based on statesHook, which is Ref in vue3 Type, a normal value in react, and a Writable type in svelte.

Responsive data

NameDescriptionTypeVersion
fetchingPreload request statusboolean-
errorRequest error messageError | undefined-
downloadingDownload progress informationObject-
uploadingUpload progress informationObject-
__referingObjInternal reference object, implement useHook compatible with different UI frameworksObject-

Operation function

NameDescriptionFunction parametersReturn valueVersion
fetchData preloading function1. method: preloaded Method instance
2. ...args: any[]
Promise-
abortInterrupt function---
updateFunction to update the current use hook front-end state, which is more useful in reactnewFrontStates: FrontRequestState-
__proxyStateInternal function, function to get state proxystateKey: stringFrameworkState

Event

NameDescriptionCallback parameterVersion
onSuccessRequest success event bindingevent: AlovaSuccessEvent-
onErrorRequest error event bindingevent: AlovaErrorEvent-
onCompleteRequest completion event bindingevent: AlovaCompleteEvent-