useRequest â
The useRequest
component is a high-level abstraction based on the useRequest
hook, allowing you to use all the powerful features of useRequest
in a declarative, component-based way.
Component Usage 2.4.0-beta
â
You can use the useRequest
component to handle data fetching, loading, error, and success states directly in your template. The API and props of the component are fully consistent with the useRequest
hook, making it easy to switch between the two approaches.
Basic Example â
<use-request
:service="fetchData"
:refreshDeps="[someDep]"
:initialData="'default'"
>
<template #default="{ data, loading, error, refresh }">
<div v-if="loading">Loading...</div>
<div v-else-if="error">Error: {{ error.message }}</div>
<div v-else>Data: {{ data }}</div>
<button @click="refresh">Refresh</button>
</template>
</use-request>
API â
The useRequest
component accepts all the same props as the useRequest
hook options:
service
: Function returning a Promise (required)refreshDeps
: Array of dependencies to trigger refreshinitialData
: Initial data valuemanual
: Whether to trigger manuallyonSuccess
,onError
,onFinally
: Lifecycle callbacks- ...and all other options supported by the
useRequest
hook
Slots â
default
: Slot props include{ data, loading, error, refresh, ... }
, matching the return values of theuseRequest
hook.loading
: Custom loading UIerror
: Custom error UI
Consistency with useRequest Hook â
All features, options, and return values of the useRequest
component are consistent with the useRequest
hook. You can refer to the useRequest hook documentation for detailed API descriptions and advanced usage.
When to Use â
- When you prefer a declarative, template-based approach for data fetching
- When you want to leverage slots for flexible UI customization
- When you need to keep logic and UI closely coupled in your component templates
Using createUseRequestComponent
â
In addition to the built-in <use-request />
component, you can use the createUseRequestComponent
utility to generate your own request components with custom types or behaviors.
Why use createUseRequestComponent
? â
- Type Safety: You can specify the data type for the request result, improving TypeScript support.
- Customization: You can create multiple request components for different data types or business scenarios.
- Encapsulation: Encapsulate specific logic or default options for your business needs.
Example â
import { createUseRequestComponent } from 'vue-hooks-plus'
// Create a typed request component
const UseRequestUserName = createUseRequestComponent<string>()
You can then use this component in your template just like <use-request />
:
<use-request-user-name
:service="fetchUserName"
:refreshDeps="[userId]"
>
<template #default="{ data, loading, error, refresh }">
<div v-if="loading">Loading...</div>
<div v-else-if="error">Error: {{ error.message }}</div>
<div v-else>User Name: {{ data }}</div>
<button @click="refresh">Refresh</button>
</template>
</use-request-user-name>
API Consistency â
The API, props, and slot props of the component created by createUseRequestComponent
are fully consistent with the useRequest
hook and the built-in <use-request />
component.
When to Use â
- When you need a request component with specific data types.
- When you want to encapsulate default options or logic for a certain business scenario.
- When you want to improve type inference and code completion in your project.