secondsky/claude-skills / plugins/tanstack-query/skills/tanstack-query
tanstack-query
VerifiedTanStack Query v5 (React Query) server state management. Use for data fetching, caching, mutations, or encountering v4 migration, stale data, invalidation errors.
examples/optimistic-update.tsx
View on GitHubimport { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
type Todo = { id: number; title: string; completed: boolean }
async function fetchTodos(): Promise<Todo[]> {
const res = await fetch('/api/todos')
if (!res.ok) throw new Error('Failed to fetch todos')
return res.json()
}
async function toggleTodo(id: number, completed: boolean) {
const res = await fetch(`/api/todos/${id}`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ completed }),
})
if (!res.ok) throw new Error('Failed to update todo')
return res.json() as Promise<Todo>
}
export function OptimisticTodos() {
const queryClient = useQueryClient()
const { data } = useQuery({
queryKey: ['todos'],
queryFn: fetchTodos,
})
const mutation = useMutation({
mutationFn: ({ id, completed }: { id: number; completed: boolean }) =>
toggleTodo(id, completed),
onMutate: async variables => {
await queryClient.cancelQueries({ queryKey: ['todos'] })
const previous = queryClient.getQueryData<Todo[]>(['todos'])
queryClient.setQueryData<Todo[]>(['todos'], old =>
(old ?? []).map(todo =>
todo.id === variables.id ? { ...todo, completed: variables.completed } : todo
)
)
return { previous }
},
onError: (_err, _vars, context) => {
if (context?.previous) {
queryClient.setQueryData(['todos'], context.previous)
}
},
onSettled: () => {
queryClient.invalidateQueries({ queryKey: ['todos'] })
},
})
return (
<ul>
{data?.map(todo => (
<li key={todo.id}>
<label>
<input
type="checkbox"
checked={todo.completed}
onChange={e =>
mutation.mutate({ id: todo.id, completed: e.target.checked })
}
/>
{todo.title}
</label>
</li>
))}
</ul>
)
}
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack." | Stars: 197k | Updated | Claude CodeCodex | Verified | Install |
| ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |