Langsung ke konten
Skillpedia
Website & UI (frontend)48k Β· #4671

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.md

Salin 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

Gunakan skill ini untuk membantu saya: https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/frontend-optimistic-mutations/SKILL.md Baca file SKILL.md di link tersebut dan ikuti instruksinya untuk tugas saya.
Mau AI Anda menemukan skill seperti ini sendiri?Pasang MCP Skillpedia gratis: cukup satu baris, dan AI mencari di antara 15.000+ skill sebelum tiap tugas.Pasang dalam 1 menit β†’

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

  1. The optimistic lifecycle is fixed. cancel β†’ snapshot β†’ patch β†’ (error: roll back) β†’ (settle: invalidate). Every optimistic mutation follows the same five beats.
  2. 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.
  3. 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.
  4. 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.
  5. 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.

repo: sickn33/agentic-awesome-skills

Popularitas

Bintang GitHub
48k
Peringkat
#4671
Lisensi
MIT
Diperbarui
15/10/2026

Apakah skill ini berguna?

0

Masuk untuk memberi suara.

Komentar

  • …

Skill lain dari repositori ini

Skill serupa