tRPC End-to-End Type Safe APIs · บทเรียน

การอัปเดตเชิงมองโลกในแง่ดีด้วยการเปลี่ยนแปลงข้อมูลของ tRPC

ทำให้ส่วนติดต่อผู้ใช้ Next.js รู้สึกตอบสนองทันทีด้วยการใช้อัปเดตเชิงมองโลกในแง่ดีกับการเปลี่ยนแปลงข้อมูลของ tRPC และย้อนกลับอย่างเรียบร้อยเมื่อคำขอล้มเหลว

บทเรียน 4 จาก 413 ขั้นตอน

การอัปเดตเชิงมองโลกในแง่ดีด้วยการเปลี่ยนแปลงข้อมูลของ tRPC เป็นบทเรียน tRPC End-to-End Type Safe APIs ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน tRPC End-to-End Type Safe APIs และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส tRPC End-to-End Type Safe APIs มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Why Optimistic Updates

When a user clicks a button, waiting for the server feels slow. Optimistic updates update the UI immediately, assuming the mutation will succeed, then reconcile with the real server response.

tRPC pairs perfectly with React Query, which handles the cache mechanics for you.

Anatomy of a tRPC Mutation

A mutation hook gives you mutate, isPending, and lifecycle callbacks: onMutate, onError, onSettled.

Optimistic logic lives in these callbacks.

const mutation = trpc.todo.add.useMutation();
mutation.mutate({ text: 'Buy milk' });

Getting the Query Utils

To touch the cache you need the tRPC utils object. It exposes per-query cancel, get, and set methods that mirror React Query.

const utils = trpc.useUtils();

Cancel In-Flight Queries

Inside onMutate, first cancel any outgoing refetches so they do not overwrite your optimistic value.

onMutate: async (newTodo) => {
  await utils.todo.list.cancel();
}

Snapshot the Previous Value

Save the current cache so you can roll back if the mutation fails.

const previous = utils.todo.list.getData();

Apply the Optimistic Change

Write the expected new state into the cache with setData. The UI updates instantly.

utils.todo.list.setData(undefined, (old) =>
  old ? [...old, { id: 'temp', text: newTodo.text }] : old
);

Return a Rollback Context

Return the snapshot from onMutate. React Query passes it to onError as context.

return { previous };

Roll Back on Error

If the server rejects the mutation, restore the snapshot so the UI matches reality.

onError: (err, newTodo, context) => {
  utils.todo.list.setData(undefined, context?.previous);
}

Reconcile on Settled

Whether it succeeds or fails, invalidate the query in onSettled so the cache resyncs with the server truth.

onSettled: () => {
  utils.todo.list.invalidate();
}

Putting It Together

The full optimistic mutation wires every callback in one place.

const add = trpc.todo.add.useMutation({
  onMutate: async (n) => {
    await utils.todo.list.cancel();
    const previous = utils.todo.list.getData();
    utils.todo.list.setData(undefined, (o) => o ? [...o, { id: 'temp', text: n.text }] : o);
    return { previous };
  },
  onError: (e, n, ctx) => utils.todo.list.setData(undefined, ctx?.previous),
  onSettled: () => utils.todo.list.invalidate(),
});

Best Practices

Keep optimistic updates safe:

  • Always cancel in-flight queries first
  • Always snapshot before mutating
  • Use a temporary id you can replace later
  • Invalidate on settle to avoid drift

Quick Check

Test your optimistic update knowledge.

Recap

You implemented optimistic updates with tRPC and React Query:

  • Cancel queries, snapshot, then setData in onMutate
  • Restore the snapshot in onError
  • Invalidate in onSettled

Your UI now feels instant while staying consistent with the server.

เริ่มต้นได้ฟรี

เรียนรู้ tRPC End-to-End Type Safe APIs ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
10
บทเรียน
40

คำถามที่พบบ่อย

บทเรียน “การอัปเดตเชิงมองโลกในแง่ดีด้วยการเปลี่ยนแปลงข้อมูลของ tRPC” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การอัปเดตเชิงมองโลกในแง่ดีด้วยการเปลี่ยนแปลงข้อมูลของ tRPC” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส tRPC End-to-End Type Safe APIs ให้อัปเกรดเป็น CoddyKit PRO คอร์ส tRPC End-to-End Type Safe APIs มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การอัปเดตเชิงมองโลกในแง่ดีด้วยการเปลี่ยนแปลงข้อมูลของ tRPC”

ทำให้ส่วนติดต่อผู้ใช้ Next.js รู้สึกตอบสนองทันทีด้วยการใช้อัปเดตเชิงมองโลกในแง่ดีกับการเปลี่ยนแปลงข้อมูลของ tRPC และย้อนกลับอย่างเรียบร้อยเมื่อคำขอล้มเหลว คุณปฏิบัติ tRPC End-to-End Type Safe APIs ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน tRPC End-to-End Type Safe APIs หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน tRPC End-to-End Type Safe APIs บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การอัปเดตเชิงมองโลกในแง่ดีด้วยการเปลี่ยนแปลงข้อมูลของ tRPC” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน tRPC End-to-End Type Safe APIs นี้ได้ไหม

ได้ บทเรียน tRPC End-to-End Type Safe APIs ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. tRPC กับตัวจัดเส้นทางแอปของ Next.js
  2. การผสานรวม React Query ขั้นสูง
  3. คอมโพเนนต์ฝั่งเซิร์ฟเวอร์และการดึงข้อมูลด้วย tRPC
  4. การอัปเดตเชิงมองโลกในแง่ดีด้วยการเปลี่ยนแปลงข้อมูลของ tRPC
← กลับไปที่ tRPC End-to-End Type Safe APIs