frontend-optimistic-mutations

Implements optimistic update lifecycle with rollback and idempotency for React query caches.

Updated Aug 11, 2026
One-click install
npx skills add https://github.com/DucCuong159/Realtime-chatapp --skill frontend-optimistic-mutations-duccuong159
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: frontend-optimistic-mutations
Source: https://github.com/DucCuong159/Realtime-chatapp/tree/main/.agent/skills/frontend-optimistic-mutations
Command: npx skills add https://github.com/DucCuong159/Realtime-chatapp --skill frontend-optimistic-mutations-duccuong159

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Frontend writes often feel slow, flicker, or leave caches inconsistent when server responses lag or fail. This Skill codifies a disciplined write path for React and React Native apps so mutations feel instant, roll back exactly on failure, and keep every cached view coherent. ## Core Features & Use Cases - Five-Beat Optimistic Lifecycle: Enforces cancel in-flight queries, snapshot caches, patch instantly, roll back verbatim on error, and invalidate on settle using TanStack Query, RTK Query, or SWR. - Cache Coherence Across Surfaces: Patches the detail cache and every affected list page together using hierarchical query key factories, so badges and lists never disagree. - Idempotency and Retry Policy: Generates idempotency keys at intent time for safe retries on money-moving writes, with network-only bounded retry rules. - Use Case: When a user marks an invoice as paid, the UI updates immediately across the detail view and all list pages; if the server rejects the write, the exact prior state is restored and the user sees a typed error. ## Quick Start Ask the AI to apply the frontend-optimistic-mutations discipline to add an optimistic status-toggle mutation with snapshot rollback and cache invalidation to your React feature module.

Frequently Asked Questions about frontend-optimistic-mutations

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I implement optimistic updates with TanStack Query?

Use the five-beat lifecycle: cancel in-flight queries in onMutate, snapshot the detail and list caches, patch them instantly, restore snapshots verbatim in onError, and invalidate queries in onSettled so server-computed fields refetch.

When should I use optimistic updates versus pending states?

Use optimistic updates for high-confidence, low-conflict writes like toggles or likes. Use pending states for creates returning server-generated ids, destructive actions requiring confirmation, or background jobs the user cannot see.

Does this optimistic mutation pattern work with RTK Query and SWR?

Yes, the same lifecycle maps to RTK Query via onQueryStarted with patchResult.undo() in catch, and to SWR via mutate with optimisticData and rollbackOnError. React Native works unchanged with all three libraries.

How do I make POST requests safe to retry with idempotency keys?

Generate the idempotency key once at form initialization using crypto.randomUUID, not inside mutationFn which re-runs per retry. The client sends it as a header so the server replays the original response for repeated keys.

Why does my UI flicker or show wrong state after a mutation?

Flicker usually means onMutate did not cancel in-flight queries, letting a late refetch overwrite the optimistic patch. Wrong state after writes often means onSettled did not invalidate, leaving stale server-computed fields, or not all list pages were patched.