Skip to main content
Version: v3

Core useHooks

useRequest

Sends 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: the hook’s configuration options.
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 managed 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 instance of the current requestMethod-
cachedResponseThe cache data that was hitany-
configThe current use hook configurationRecord<string, any>-
argsThe arguments passed to the response callback (provided via send)any[]-
proxyStatesA collection of the hook’s state proxies, such as data, loading, and error.FrameworkStateMap-
sendSends the request.(...args: any[]) => voidPromise
abortAborts the request.() => 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
loadingWhether the request is loadingboolean-
dataResponse dataany-
errorThe request errorError | 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 instance of the current requestMethod-
argsThe arguments passed to the response callback (provided via send)any[]-
errorResponse error instanceError-

AlovaCompleteEvent

NameDescriptionTypeVersion
methodThe method instance of the current requestMethod-
argsThe arguments passed to the response callback (provided via send)any[]-
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 reactive data type of the corresponding UI framework based on statesHook, which is Ref type in Vue 3, plain value in React, and Writable type in svelte.

Responsive data

NameDescriptionTypeVersion
loadingWhether the request is loadingboolean-
dataResponse dataany-
errorThe request errorError | undefined-
downloadingDownload progress informationObject-
uploadingUpload progress informationObject-
__referingObjinternal reference object that makes the hook compatible with different UI frameworksObject-

Operation function

NameDescriptionFunction parametersReturn valueVersion
sendSends the request....args: any[]--
abortAborts the request.-Promise-
updateupdates the hook’s front-end state. More useful in React.newFrontStates: 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 state and sends a request when it 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: the hook’s configuration options.

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 managed states, can be updated through updateStateRecord<string | number | symbol, any>-
debounceThe debounce time in 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 abort the last pending requestbooleantrue-
__referingObjinternal reference object that makes the hook compatible 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 reactive data type of the framework is Ref in Vue 3, plain value in React, and Writable in Svelte.

Responsive data

NameDescriptionTypeVersion
loadingWhether the request is loadingboolean-
dataResponse dataany-
errorThe request errorError | undefined-
downloadingDownload progress informationObject-
uploadingUpload progress informationObject-
__proxyStateInternal function, function to get state proxystateKey: stringFrameworkState

Operation function

NameDescriptionFunction parametersReturn valueVersion
sendSends the request....args: any[]Promise-
abortAborts the request.---
updateUpdates the hook’s front-end state. More useful in React.newFrontStates: 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: the hook’s configuration options.
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 that makes the hook compatible with different UI frameworksObject-

AlovaFetcherMiddlewareContext

NameDescriptionTypeVersion
methodThe method instance of the current requestMethod-
cachedResponseThe cache data that was hitany-
configCurrent use hook configurationRecord<string, any>-
argsResponse processing callback parameters, which are passed in by useFetcher's fetchany[]-
proxyStatesA collection of the hook’s preload state proxies, such as loading and error.FetchRequestState-
fetchPreloads data.(method: Method, ...args: any[]) => voidPromise
abortAborts the request.() => 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 reactive data type of the corresponding UI framework based on statesHook, which is Ref type in Vue 3, normal value in react, and Writable type in svelte

NameDescriptionTypeVersion
loadingPreload request statusboolean-
errorThe request errorError | 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 reactive data type of the corresponding UI framework based on statesHook, which is Ref in Vue 3 Type, a normal value in react, and a Writable in Svelte.

Responsive data

NameDescriptionTypeVersion
fetchingPreload request statusboolean-
errorThe request errorError | undefined-
downloadingDownload progress informationObject-
uploadingUpload progress informationObject-
__referingObjinternal reference object that makes the hook compatible with different UI frameworksObject-

Operation function

NameDescriptionFunction parametersReturn valueVersion
fetchData preloading function1. method: preloaded Method instance
2. ...args: any[]
Promise-
abortAborts the request.---
updateUpdates the hook’s front-end state. More useful in React.newFrontStates: 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-