reactive/data-client / .agents/skills/data-client-vue
data-client-vue
UnverifiedRead, change and render async and shared state in Vue with @data-client/vue: any data from an API or other async source, and global state shared across components (it has to persist somewhere, which makes it async). Use it for loading and error states, mutations and forms, filtered or derived views, and debugging data that looks wrong or stale on screen. Use it in place of fetch in onMounted or watch, or app-wide refs and provide/inject; its main composables are useSuspense, useQuery and useController.
references/useLoading.md
View on GitHub<!-- Generated by `yarn build:skills` from docs/core/api/useLoading.md (vue). Edit the source doc, not this file. -->
# useLoading()
Helps track loading and error state of imperative async functions.
> **Tip**
>
> [useSuspense()](./useSuspense.md) or [useDLE()](./useDLE.md) are better for GET/read endpoints.
## Usage
Files with a `route` are pages mounted at that path, with its params as props (vue-router `props: true`).
```ts title="PostResource"
import { Entity, resource } from '@data-client/rest';
export class Post extends Entity {
id = 0;
author = 0;
title = '';
body = '';
votes = 0;
static key = 'Post';
}
export const PostResource = resource({
path: '/posts/:id',
schema: Post,
});
```
```html title="PostDetail.vue" route="/posts/:id"
<script setup lang="ts">
import { computed } from 'vue';
import { useSuspense } from '@data-client/vue';
import { PostResource } from './PostResource';
const props = defineProps<{ id: string }>();
const post = await useSuspense(PostResource.get, computed(() => ({
id: props.id,
})));
</script>
<template>
<article>
<h2>{{ post.title }}</h2>
<p>{{ post.body }}</p>
</article>
</template>
```
```html title="PostForm.vue"
<script setup lang="ts">
defineProps<{
loading: boolean;
error: Error | undefined;
}>();
const emit = defineEmits<{ submit: [data: FormData] }>();
const handleSubmit = (e: Event) => {
e.preventDefault();
emit('submit', new FormData(e.target as HTMLFormElement));
};
</script>
<template>
<form @submit="handleSubmit" :aria-busy="loading">
<fieldset :disabled="loading">
<label class="field">
Title
<input type="text" name="title" value="My New Post" required />
</label>
<label class="field">
Body
<textarea
name="body"
rows="12"
required
value="After clicking 'save', the form is locked and the button animates until the POST completes. Upon completion the newly created post is displayed immediately as Reactive Data Client is able to use the fetch response to populate the store."
/>
</label>
<div v-if="error" class="alert alert--danger">{{ error.message }}</div>
<button type="submit">{{ loading ? 'Saving…' : 'Save' }}</button>
</fieldset>
</form>
</template>
```
```html title="PostCreate.vue" route="/"
<script setup lang="ts">
import { useLoading, useController } from '@data-client/vue';
import { useRouter } from 'vue-router';
import { PostResource } from './PostResource';
import PostForm from './PostForm.vue';
const ctrl = useController();
const router = useRouter();
const [handleSubmit, loading, error] = useLoading(
async (data: FormData) => {
const post = await ctrl.fetch(PostResource.getList.push, data);
router.push(`/posts/${post.id}`);
},
);
</script>
<template>
<PostForm @submit="handleSubmit" :loading="loading" :error="error" />
</template>
```
Returns the wrapped function along with `loading` and `error` [refs](https://vuejs.org/api/reactivity-core.html#ref).
The wrapped function is stable, so no dependency list is needed: any refs or props it reads are
read at call time.
## Types
```typescript
export default function useLoading<
F extends (...args: any) => Promise<any>,
>(func: F): [F, Ref<boolean>, Ref<Error | undefined>];
```
`loading` is `true` while the returned promise is pending. If `func` rejects, the rejection is
caught and stored in `error`; it is cleared again on the next call. When calls overlap, `loading` and
`error` follow the latest call, so an earlier call that settles later doesn't change them.
## Examples
### Todo creation
Example app: [vue-todo-app](https://github.com/reactive/data-client/tree/master/examples/vue-todo-app) ([`src/components/TodoList.vue`](https://github.com/reactive/data-client/blob/master/examples/vue-todo-app/src/components/TodoList.vue), [`src/resources/TodoResource.ts`](https://github.com/reactive/data-client/blob/master/examples/vue-todo-app/src/resources/TodoResource.ts))
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| brainstormingobra/superpowersYou MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation). | Stars: 319k | Updated | Claude CodeCodex | Verified | Install |
| brand-guidelinesanthropics/skillsApplies Anthropic's official brand colors and typography to any sort of artifact that may benefit from having Anthropic's look-and-feel. Use it when brand colors or style guidelines, visual formatting, or company design standards apply. | Stars: 196k | Updated | Claude CodeCodex | Verified | Install |
| frontend-designanthropics/skillsGuidance for distinctive, intentional visual design when building new UI or reshaping an existing one. Helps with aesthetic direction, typography, and making choices that don't read as templated defaults. | Stars: 196k | Updated | Claude CodeCodex | Verified | Install |
| mcp-builderanthropics/skillsGuide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when building MCP servers to integrate external APIs or services, whether in Python (FastMCP) or Node/TypeScript (MCP SDK). | Stars: 196k | Updated | Claude CodeCodex | Verified | Install |