frontend-optimistic-mutations
Fungsinya
Skill ini membantu membuat pembaruan di layar terasa instan bagi pengguna. Skill ini langsung memperbarui antarmuka dan, jika terjadi kesalahan, membatalkan perubahan tersebut tanpa jejak.
Kapan dipakai
- βSaat menandai item sebagai selesai.
- βSaat mengubah status sebuah pesanan.
- βSaat mengurutkan ulang daftar item.
Link skill
https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/frontend-optimistic-mutations/SKILL.mdSalin lalu tempelkan ke AI Anda (Claude, ChatGPT, atau Cursor) dan minta AI mengikuti instruksinya.
Cara pakai
Cara paling mudah: salin pesan ini dan tempelkan ke AI Anda. AI akan membaca skill dan mengikuti instruksinya.
Pesan siap tempel
Untuk Claude Code, Codex, atau Cursor: jalankan di folder proyek, lalu skill langsung terpasang.
Unduh SKILL.md lalu taruh di folder skill agen Anda.
Contoh permintaan ke AI
βBuat agar menandai invoice sebagai 'lunas' terasa instan, tetapi kembalikan ke kondisi semula jika ada error.β
5 skill baru setiap minggu. 1 menit baca.
Cara kerjanya
Saat Anda melakukan perubahan, layar langsung diperbarui. Jika perubahan gagal dikirim ke server, layar akan kembali ke kondisi sebelumnya seolah-olah tidak ada yang terjadi.
Konten teknis (SKILL.md asli)
Teks asli dari pembuat, dalam bahasa Inggris, langsung dari GitHub.
Frontend Optimistic Mutations (the write path)
When to Use
Use this skill when you need a portable, framework-agnostic discipline for the write path of any React or React Native app using a query/cache layer. Codifies the optimistic-update lifecycle (cancel in-flight queries β snapshot every affected cache β patch instantly β roll back verbatim on error β invalidate on...
Portable skill β readable by Claude Code, OpenCode, Codex, Cursor, Windsurf, and others. This skill describes the discipline of the write path β optimistic updates, rollback, idempotency, cache coherence β not a UI library or a styling system. It builds directly on the frontend-data-contracts skill (writes go through the typed client) and the frontend-architecture skill (mutations live in
modules/{feature}/hooks/, keyed by a factory).
The goal: a write feels instant (the UI reflects it before the server confirms), is safe (a failure restores the exact prior state, and a retry never double-charges), and leaves the cache coherent (the detail view and every list page agree). All three at once β that's the craft.
0. The five core ideas
- The optimistic lifecycle is fixed. cancel β snapshot β patch β (error: roll back) β (settle: invalidate). Every optimistic mutation follows the same five beats.
- Roll back verbatim. On failure, restore the exact snapshot taken before the patch β not a "best guess" re-derivation. Keep the snapshot in mutation context.
- Idempotency is generated once, not per attempt. The key is created at form init (or first intent), so a network retry replays the original server response instead of performing the action twice.
- Caches move in lock-step. A status change patches the detail cache and every list page that contains the entity, so badges never disagree across surfaces.
- Server state never enters the client store. Optimistic state lives in the query cache, not Zustand/Redux. The cache is the single source of truth for server data (per frontend-architecture Β§4).
1. When to be optimistic (and when not)
| Situation | Strategy |
|---|---|
| High-confidence, low-conflict write (toggle status, like, mark-paid, reorder) | Optimistic β patch immediately, roll back on error. |
| Create that returns a server-generated id/number/total | Pending state, then setQueryData from the server response. A temporary optimistic row is optional; reconcile on success. |
| Destructive or hard-to-reverse write (delete with cascade, send money) | Confirm first, then optimistic or pending β never silent-optimistic. |
| Write whose result the user can't see yet (background job) | Pending + toast, invalidate when done. No optimistic patch. |
Optimism is a UX tool for writes you're confident will succeed. If failure is common or expensive to undo, prefer a pending state.
2. The optimistic lifecycle (TanStack Query)
The canonical shape. Each beat has a job; skipping one breaks correctness.
// modules/invoice/hooks/useInvoiceMutations.ts
interface MarkPaidContext {
previousInvoice: Invoice | undefined; // detail snapshot
previousLists: Array<[readonly unknown[], InvoiceListResponse]>; // every list page snapshot
}
export function useMarkInvoicePaid() {
const queryClient = useQueryClient();
const notifyError = useApiErrorToast();
return useMutation<Invoice, ApiError, { id: InvoiceId }, MarkPaidContext>({
mutationFn: ({ id }) => apiClient.post<Invoice>(INVOICE_API.markPaid(id)),
// 1 + 2 + 3: cancel in-flight reads, snapshot, patch
onMutate: async ({ id }) => {
await queryClient.cancelQueries({ queryKey: invoiceKeys.all }); // (1) no late refetch clobber
const detailKey = invoiceKeys.detail(id);
β¦
Pertanyaan umum
Apa maksud 'optimistic update'?
Ini adalah ketika antarmuka pengguna langsung diperbarui untuk menampilkan perubahan, bahkan sebelum server mengonfirmasi bahwa perubahan berhasil. Ibaratnya seperti mengisi formulir dan melihat tulisan 'tersimpan' muncul sebelum sistem benar-benar menyimpannya.
Apa itu 'rollback'?
Rollback adalah kemampuan untuk mengembalikan perubahan ke kondisi persis seperti sebelumnya jika terjadi error. Dengan begitu, pengguna tidak dibiarkan dengan kondisi data yang tidak konsisten atau salah.
Mengapa daftar dan halaman detail harus sinkron?
Supaya informasi tetap konsisten di seluruh aplikasi. Jika Anda menandai sebuah item sebagai lunas di daftar, semua tempat lain yang menampilkan item tersebut (misalnya halaman detail) juga sebaiknya langsung menampilkan perubahan itu.
Detail
Deskripsi asli dari pembuat
A portable, framework-agnostic discipline for the write path of any React or React Native app using a query/cache layer.
Popularitas
- Bintang GitHub
- 48k
- Peringkat
- #4671
- Lisensi
- MIT
- Diperbarui
- 15/10/2026
Komentar
- β¦
Skill lain dari repositori ini
00-andruia-consultant
48kMembantu agent Anda memulai atau meningkatkan proyek AI. Agent menganalisis kode, menyarankan tools, dan membuat rencana aksi dalam bahasa Spanyol.
007
48kMemungkinkan agent Anda melakukan audit keamanan dan threat modeling. Berguna untuk melindungi proyek dari serangan dan celah keamanan.
10-andruia-skill-smith
48kMembantu agent Anda membuat atau meningkatkan tools AI. Agent mendefinisikan cara kerja tools, menulis dokumentasi, dan memvalidasi strukturnya.
20-andruia-niche-intelligence
48kMemungkinkan agent Anda membuat ringkasan industri tertentu. Berguna untuk memahami konteks, aturan, pola penggunaan, dan risiko proyek dengan sumber terpercaya.
Skill serupa
angular-developer
280kMembantu agent Anda membuat kode Angular dan memberi saran struktur proyek. Berguna saat memulai proyek baru, membuat komponen, atau meningkatkan performa.
frontend-a11y
280kMembantu agent Anda membuat antarmuka web dapat diakses semua orang. Memastikan website dan aplikasi berfungsi baik dengan screen reader dan navigasi keyboard.
frontend-patterns
280kMembuat agent Anda menerapkan pola modern dalam frontend. Mengoptimalkan performa, mengelola state, dan mengikuti praktik terbaik React dan Next.js.