data-provider
БесплатноБез исполняемых скриптовНе проверенTeaches the data provider pattern using renderless components and scoped slots. Use when you need to abstract data fetching or state management logic and…
от PatternsDev★ 261 звезда на GitHubОб этом скилле
Data Provider Pattern
Table of Contents
In a previous article, we've come to learn how renderless components help separate the logic of a component from its presentation. This becomes useful when we need to create reusable logic that can be applied to different UI implementations.
Renderless components also allow us to leverage another helpful pattern known as the data provider pattern.
When to Use
- Use this when multiple components need to consume the same data but display it differently
- This is helpful for centralizing data-fetching logic without coupling it to specific UI components
When NOT to Use
- When composables can handle the data logic without the extra component layer (Vue 3+)
- When only one component consumes the data — a composable or inline fetch is simpler
- When the data-provider nesting adds indirection that makes the template harder to follow
Instructions
- Create a data provider component whose template is a single
<slot>with scoped slot props - Pass data, loading state, and action methods as scoped slot props to child components
- Use
v-slotdestructuring in the parent to access provided data - Keep child components focused purely on presentation; the data provider handles all data logic
Details
Data Provider Pattern
The data provider pattern is a design pattern that complements the renderless component pattern in Vue by focusing on providing data and state management capabilities to components without being concerned about how the data is rendered or displayed.
In the data provider pattern, a data provider component encapsulates the logic for fetching, managing, and exposing data to its child components. The child components can then consume this data and use it in their own rendering or behavior.
This pattern promotes separation of concerns, as the data provider component takes care of data-related tasks, while the child components can focus on presentation and interaction.
Let's illustrate the data provider pattern with an example. Consider a simple application that displays the setup of a funny joke followed by its punchline. To keep the example self-contained, we'll use a local in-memory data source instead of depending on an external API.
const jokes = [
{ id: 1, setup: "Why did the dev go broke?", punchline: "Because they used up all their cache." },
{ id: 2, setup: "Why do functions love TypeScript?", punchline: "Because it keeps their arguments in order." },
];
We'll first create a data provider component called DataProvider that will hold the responsibility of loading a joke. In the <script> section of the component, we'll import the ref() and reactive() functions from Vue, define a local data source, and set up data and loading reactive properties to capture the selected joke and loading state.
<script setup>
import { ref, reactive } from "vue";
const jokes = [
{ id: 1, setup: "Why did the dev go broke?", punchline: "Because they used up all their cache." },
{ id: 2, setup: "Why do functions love TypeScript?", punchline: "Because it keeps their arguments in order." },
];
const data = reactive({
setup: null,
punchline: null,
});
const loading = ref(false);
</script>
We can then create a fetchJoke() function in our DataProvider component to simulate loading data asynchronously.
const fetchJoke = async () => {
loading.value = true;
try {
await new Promise((resolve) => setTimeout(resolve, 300));
const jokeData = jokes[Math.floor(Math.random() * jokes.length)];
data.setup = jokeData.setup;
data.punchline = jokeData.punchline;
} catch (error) {
console.error("Error loading joke:", error);
} finally {
loading.value = false;
}
};
With the fetch function ready, we can call it when the component mounts using the onMounted() lifecycle hook.
import { ref, reactive, onMounted } from "vue";
// ...
onMounted(() => {
fetchJoke();
});
The key element in a data provider component is that its template consists purely of a single <slot> element. This slot will provide the fetched data and the relevant method to its child components using scoped slots.
<template>
<slot :data="data" :loading="loading" :fetchJoke="fetchJoke"></slot>
</template>
The DataProvider component passes data, loading, and fetchJoke as scoped slot props. This means any child component placed inside the DataProvider can access these properties.
Now, let's create a JokeCard component that will present the joke data.
<template>
<div class="joke-card">
<p v-if="loading">Loading...</p>
<div v-else>
<p class="setup">{{ data.setup }}</p>
<p class="punchline">{{ data.punchline }}</p>
</div>
<button @click="fetchJoke">Get Another
Установить data-provider в Claude Code и Claude Desktop
Зарегайся, чтобы установить скилл
Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.
- Открой команду установки в одну строку
- Сохраняй скиллы в синхронизируемую библиотеку
- Уведомления, когда скиллы обновляются
Разрешённые инструменты
Инструменты, которые скиллу разрешено вызывать.
Без ограничений — скилл может использовать любой инструмент.
FAQ
Что делает скилл data-provider?
Teaches the data provider pattern using renderless components and scoped slots. Use when you need to abstract data fetching or state management logic and expose it to child components via slots.
Как установить скилл data-provider?
Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.
Скилл data-provider запускает скрипты?
Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.
Похожие скиллы
skill-creator
Create new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an…
от Anthropicdiscernment-nudge
After you give a substantive answer or draft that the user may act on — advice or recommendations, drafted artifacts such as goals, plans, pitches, proposals…
от Anthropicdata-consolidate
Joins the plant's scheduled exports and connector data into one working dataset — ERP items (NetSuite or Infor M3), sales orders, production work orders…
от Anthropicinspection-to-capa
Runs the quality chain end to end: writes the nonconformance against the requirement it violates, searches history at the moment of writing to say whether…
от Anthropic