useRequest
Production-ready Vue Hook to manage asynchronous data.
The API is different from ahooks.
Examples
Basic usage
Manual trigger
Polling
- You can set
options.pollingWhenHidden=falseto temporarily suspend the scheduled task when the screen is not visible. - Use
run/cancelto start / stop polling. - When
options.manual=true, you need to call therunfunction to start the polling - When
options.pollingSinceLastFinished=false, request will start everypollingIntervaltime, not waitting last request finished.
Debounce
- When
options.loadingWhenDebounceStart=false, loading will not betrueimmediately, wait until the function actually executes.
Throttle
Loading Delay
Basic API
const {
data,
error,
loading,
run,
params,
cancel,
refresh,
fetches,
lastSuccessParams,
} = useRequest(service, {
formatResult,
manual,
onSuccess,
onError,
defaultLoading,
loadingDelay,
defaultParams,
pollingInterval,
pollingWhenHidden,
pollingSinceLastFinished,
refreshOnWindowFocus,
focusTimespan,
debounceInterval,
loadingWhenDebounceStart,
throttleInterval,
initialData,
requestMethod,
ready,
throwOnError,
refreshDeps, // v0.9
refreshOnWindowFocus, // v0.9
});
Result
| Property | Description | Type |
|---|---|---|
| data |
| undefined / any |
| error | exception thrown by service, default is undefined | undefined / Error |
| loading | Whether the service is loaded | boolean |
| run |
| (...args: any[]) => Promise |
| params | An array of parameters for the service being executed. For example, you triggered run (1, 2, 3), then params is equal to [[1, 2, 3] | any[] |
| lastSuccessParams | Assigned onlt when last service success | any[] |
| cancel |
| () => void |
| refresh | Using the last params, re-execute the service | () => Promise |
Params
All Options are optional.
| Property | Description | Type | Default |
|---|---|---|---|
| manual |
| boolean | false |
| initialData | initial data | any | - |
| formatResult | Format request results | (response: any) => any | - |
| onSuccess |
| (data: any, params: any[]) => void | - |
| onError | Triggered when the service reports an error. The parameters are error andparams. | (error: Error, params: any[]) => void | - |
| defaultParams | If manual = false, the default parameters when run is executed automatically, | any[] | - |
| loadingDelay | Set delay time for display loading to avoid flicker | number | - |
| pollingInterval | Polling interval in milliseconds. After setting, it will enter polling mode and trigger run periodically. | number | - |
| pollingSinceLastFinished |
| boolean | true |
| pollingWhenHidden |
| boolean | true |
| debounceInterval | debounce interval, the unit is millisecond. After setting, request to enter debounce mode. | number | - |
| loadingWhenDebounceStart | Whether set loading to true when the run function starts to execute. | boolean | true |
| throttleInterval | throttle interval, the unit is millisecond. After setting, request to enter throttle mode. | number | - |
| throwOnError | If the service errors, the error will only be logged. If you want an error to be thrown, pass the throwOnError: true | boolean | false |
refreshOnWindowFocus v0.9 | The request will be re-initiated when the screen is refocused or revisible. | boolean | false |
refreshDeps v0.9 | RefreshDeps changes will trigger the service to re-execute. | WatchSource[] | [] |