Thuta Learning
ရှာဖွေရန်
IntermediateWeb Developmentintermediate

Loading၊ Error နှင့် Not Found UI

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • loading.tsx ဖြင့် instant feedback ပေးရန်
  • error.tsx ဖြင့် route error ကိုဖမ်းရန်
  • not-found.tsx နှင့် notFound ကိုသုံးရန်

User က link နှိပ်ပြီး ဘာမှမပြောင်းဘဲ တိတ်နေတဲ့နှစ်စက္ကန့်ဟာ app ပျက်သွားပြီလို့ ထင်စေပါတယ်။ Loading၊ expected not-found နဲ့ unexpected error ကို ခွဲပြရင် app က ပိုယုံကြည်ရပါတယ်။

နားလည်ထားရမယ့် အချက်

loading.tsx က route segment အတွက် fallback UI ဖြစ်ပြီး navigation ချက်ချင်းတုံ့ပြန်စေပါတယ်။ notFound() က data မရှိတာလို မျှော်မှန်းနိုင်တဲ့ 404 အခြေအနေကို not-found.tsx ဆီပို့ပါတယ်။ error.tsx က မမျှော်လင့်တဲ့ exception ကို error boundary နဲ့ဖမ်းပြီး retry လုပ်ခွင့်ပေးပါတယ်။ Error component က interactivity လိုလို့ Client Component ဖြစ်ရပါတယ်။

အတူတူ စမ်းရေးကြည့်မယ်

tsx
// app/notes/loading.tsx
export default function Loading() {
  return <p>မှတ်စုများ ဖွင့်နေသည်…</p>;
}

// app/notes/error.tsx
"use client";
export default function ErrorPage({
  unstable_retry,
}: {
  error: Error;
  unstable_retry: () => void;
}) {
  return <button onClick={unstable_retry}>ပြန်စမ်းကြည့်မယ်</button>;
}

// app/notes/not-found.tsx
export default function NotFound() {
  return <h2>ဒီမှတ်စုကို မတွေ့ပါ</h2>;
}

Code က ဘယ်လိုအလုပ်လုပ်သလဲ

Files သုံးခုကို route folder တစ်ခုတည်းမှာထားပါ။ Loading က data စောင့်ချိန်၊ not-found က record မရှိချိန်၊ error က exception ဖြစ်ချိန် ပေါ်လာပါတယ်။ အားလုံးကို “တစ်ခုခုမှားနေသည်” တစ်မျိုးတည်းမပြသင့်ပါဘူး။

You should see
Loading၊ retryable error နှင့် 404 အခြေအနေတိုင်းအတွက် သင့်တော်သော UI ပေါ်မည်။

၅ မိနစ် စမ်းကြည့်

Loading skeleton card နှစ်ခုရေးပြီး error UI မှာ user နားလည်မယ့်စာသားနဲ့ Home ပြန်သွားရန် link ထည့်ပါ။

Next.js — Error HandlingNext.js

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • မျှော်မှန်းနိုင်သော validation error ကို exception အဖြစ်ပစ်ခြင်း
  • error.tsx မှာ use client မထည့်ခြင်း

လေ့ကျင့်ခန်း

Loading skeleton card နှစ်ခုရေးပြီး error UI မှာ user နားလည်မယ့်စာသားနဲ့ Home ပြန်သွားရန် link ထည့်ပါ။

You'll know it worked when: Loading၊ retryable error နှင့် 404 အခြေအနေတိုင်းအတွက် သင့်တော်သော UI ပေါ်မည်။

Loading၊ Error နှင့် Not Found UI | Thuta Learning