Thuta Learning
React
AdvancedWeb Developmentအလယ်အလတ်

Suspense, Lazy Loading & Error Boundaries

Reactသင်ခန်းစာ 2135 မိနစ်

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

  • lazy(() => import(...)) နဲ့ component တစ်ခုကို သီးခြား chunk အဖြစ် ခွဲပြီး <Suspense fallback> နဲ့ loading UI ပြနိုင်ဖို့
  • Component တစ်ခု suspend ဖြစ်ရင် အနီးဆုံး Suspense boundary က fallback ကို ပြတယ်ဆိုတာ ရှင်းပြနိုင်ဖို့
  • getDerivedStateFromError နဲ့ componentDidCatch ပါတဲ့ error boundary class component ကို ရေးနိုင်ဖို့
  • Error boundary က ဘာ error တွေကို ဖမ်းပြီး ဘာတွေကို မဖမ်းဘူးဆိုတာ ခွဲခြားနိုင်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

Suspense နဲ့ error boundary ကို ရိုးရိုးလေး ပြောရရင်

App ကြီးလာရင် user အများစု မဖွင့်တဲ့ Reports page လို အပိုင်းတွေရဲ့ code ကို ပထမ load မှာ ထည့်မပို့ချင်ပါဘူး။ lazy() က 'ဒီ component ကို ပထမဆုံး ပြရမယ့်အချိန်ကျမှ download လုပ်' လို့ ပြောပါတယ်။ Download လုပ်နေတုန်း screen အလွတ် မဖြစ်အောင် <Suspense> က 'Loading…' လို fallback ကို ယာယီ ပြပေးပါတယ်။

Error boundary ကတော့ အိမ်ရဲ့ fuse box လိုပါပဲ။ အခန်းတစ်ခန်းမှာ short ဖြစ်ရင် အိမ်တစ်အိမ်လုံး မီးမပျက်ဘဲ အဲဒီ fuse တစ်ခုပဲ ပြတ်ပါတယ်။ Component တစ်ခု render လုပ်ရင်း crash ဖြစ်ရင် boundary က အဲဒီ အပိုင်းကို 'Could not load reports.' လို message နဲ့ အစားထိုးပြီး ကျန်တဲ့ app ဆက်အလုပ်လုပ်ပါတယ်။

Problem → Why → Solution

ပြဿနာ: widget တစ်ခုရဲ့ bug က screen တစ်ခုလုံးကို ဖြူစေခြင်း

Dashboard မှာ widget ဆယ်ခု ရှိပြီး PriceWidget တစ်ခုက API ကနေ မမျှော်လင့်ထားတဲ့ data ရလာလို့ render လုပ်ရင်း error throw လုပ်ပါတယ်။ Error boundary မရှိရင် React က root တစ်ခုလုံးကို unmount လုပ်လို့ user က screen ဖြူကြီးကိုပဲ မြင်ရပါတယ် — widget ကိုးခု ကောင်းနေတာတောင် မမြင်ရတော့ပါဘူး။ တစ်ချိန်တည်းမှာ Reports page ရဲ့ chart library ကြီးကလည်း home page ဖွင့်တာနဲ့ download ဖြစ်နေလို့ ပထမ load နှေးနေပါတယ်။

Dashboard.jsx
import Reports from './Reports.jsx'; // big chart code, loaded up front

function PriceWidget({ price }) {
  if (price < 0) throw new Error('price cannot be negative');
  return <p>Price: {price} USD</p>;
}

export default function Dashboard({ price }) {
  return (
    <>
      <PriceWidget price={price} /> {/* one throw -> the whole app unmounts */}
      <Reports />
    </>
  );
}

ဘာကြောင့် app တစ်ခုလုံး ပျက်သွားတာလဲ

Render လုပ်နေစဉ် error တစ်ခု ဖြစ်ရင် React က အဲဒီ UI ကို ဆက်ပြဖို့ မလုံခြုံတော့ဘူးလို့ ယူဆပါတယ်။ ဖမ်းမယ့် error boundary တစ်ခုမှ မရှိရင် ပျက်နေတဲ့ UI ကို ပြနေမယ့်အစား component tree တစ်ခုလုံးကို ဖယ်ရှားပါတယ်။ Error boundary တွေက 'ဒီအပိုင်း ပျက်ရင် ဒီ fallback ကို ပြ' လို့ နယ်နိမိတ် သတ်မှတ်ပေးပြီး lazy + Suspense ကတော့ code ကို လိုမှ download လုပ်ပေးပါတယ်။

Core concept: loading နဲ့ error အတွက် boundary များ

lazy(() => import('./Reports.jsx')) က Reports ကို ပထမဆုံး render လုပ်ရမယ့်အချိန်မှ import() ကို ခေါ်ပါတယ်။ Vite လို bundler က dynamic import တစ်ခုစီကို သီးခြား JavaScript file (chunk) အဖြစ် ခွဲထုတ်ပါတယ်။ Chunk မရောက်သေးခင် Reports က 'suspend' ဖြစ်ပါတယ် — render ဆက်မလုပ်နိုင်သေးဘူးလို့ React ကို ပြောတာပါ။ React က tree ထဲမှာ အထက်ဘက်ကို ရှာပြီး အနီးဆုံး <Suspense> ရဲ့ fallback ကို ပြကာ chunk ရောက်ရင် Reports ကို ထပ်ကြိုးစား render လုပ်ပါတယ်။ lazy ကို module ရဲ့ top level မှာ ကြေညာရပါမယ်၊ component ထဲမှာ ကြေညာရင် render တိုင်း component type အသစ် ဖြစ်သွားပါတယ်။

Suspense က lazy code အတွက်ပဲ မဟုတ်ပါဘူး၊ React ရဲ့ use() နဲ့ cache လုပ်ထားတဲ့ promise ကို ဖတ်တာ ဒါမှမဟုတ် Suspense ကို support လုပ်တဲ့ framework နဲ့ library တွေရဲ့ data loading လည်း suspend ဖြစ်စေနိုင်ပါတယ်။ ဒါပေမဲ့ useEffect ထဲမှာ fetch လုပ်တာကတော့ Suspense ကို လုံးဝ မထိပါဘူး — အဲဒီ loading ကို state နဲ့ ကိုယ်တိုင် ပြရပါတယ်။

Error boundary ဆိုတာ static getDerivedStateFromError(error) ဒါမှမဟုတ် componentDidCatch(error, info) (ဒါမှမဟုတ် နှစ်ခုလုံး) ပါတဲ့ class component တစ်ခုပါ။ အောက်က component တစ်ခုခု render လုပ်ရင်း၊ lifecycle method ဒါမှမဟုတ် effect ထဲမှာ throw လုပ်ရင် getDerivedStateFromError က fallback ပြဖို့ state ကို ပြောင်းပြီး componentDidCatch က error ကို log လုပ်ဖို့ နေရာ ဖြစ်ပါတယ်။ Hook နဲ့ ရေးတဲ့ error boundary မရှိသေးလို့ class တစ်ခု ကိုယ်တိုင် ရေးရမယ်၊ ဒါမှမဟုတ် react-error-boundary လို library ကို သုံးရပါမယ်။

Boundary တစ်ခုက သူ့ အောက်က component တွေရဲ့ error ကိုပဲ ဖမ်းပါတယ်၊ boundary ကိုယ်တိုင်ရဲ့ render ထဲက error ကတော့ အထက်က နောက် boundary ဆီ ဆက်တက်သွားပါတယ်။ ဒါကြောင့် fallback UI ကို ရိုးရှင်းအောင် ထားပါ။ Boundary ရဲ့ error state ကလည်း သူ့ဘာသာ မပျောက်ပါဘူး — reset လုပ်ပေးမှ သို့မဟုတ် key ပြောင်းပြီး ပြန် mount လုပ်မှ children ကို ပြန်ပြပါတယ်။

Error ဖြစ်တဲ့ နေရာBoundary က ဖမ်းလား
Render, lifecycle method, effectဖမ်းတယ်
lazy() import မအောင်မြင်ခြင်းဖမ်းတယ်
onClick လို event handlerမဖမ်းဘူး၊ try/catch သုံးပါ
setTimeout, render နဲ့ မဆိုင်တဲ့ promiseမဖမ်းဘူး
Boundary နှစ်မျိုး၊ ပြဿနာနှစ်မျိုး
  1. Child က suspend -> အနီးဆုံး Suspense
  2. Fallback ပြ၊ ready ဖြစ်ရင် ပြန် render
  3. Child က throw -> အနီးဆုံး error boundary
  4. Boundary က fallback ပြ၊ ကျန်တာ ဆက်run

Boundary တွေကို ဘယ်နေရာ ထားမလဲ ဆိုတာက 'ဘယ်လောက် အပိုင်းကို တစ်ပြိုင်နက် loading/error ပြမလဲ' ဆိုတဲ့ design ဆုံးဖြတ်ချက်ပါ။

အထွေထွေ ပုံစံ

jsx
const LazyPart = lazy(() => import('./SomePart.jsx')); // module top level

class ErrorBoundary extends Component {
  state = { hasError: false };
  static getDerivedStateFromError() { return { hasError: true }; }
  render() { return this.state.hasError ? fallbackUI : this.props.children; }
}

<ErrorBoundary>
  <Suspense fallback={loadingUI}><LazyPart /></Suspense>
</ErrorBoundary>

Lazy component ကို module အပေါ်ဆုံးမှာ ကြေညာပါ၊ Suspense ကို loading ပြမယ့် နေရာမှာ ထားပြီး error boundary ကို အပြင်ဘက်က ထုပ်ပါ — ဒါမှ chunk download မအောင်မြင်တဲ့ error ကိုပါ ဖမ်းနိုင်ပါတယ်။

Worked example: lazy Reports page ကို ကာကွယ်ထားခြင်း

Reports component ကို ကိုယ်ပိုင် chunk အဖြစ် ခွဲထားပြီး Suspense က download လုပ်နေစဉ် loading message ပြပါတယ်။ Error boundary က chunk download မအောင်မြင်တာ (network ပြတ်တာ၊ deploy အသစ်ကြောင့် file ပျောက်တာ) ဒါမှမဟုတ် Reports ကိုယ်တိုင် render လုပ်ရင်း crash ဖြစ်တာကို ဖမ်းပြီး alert message ပြပါတယ်။

App.jsx
import { Component, lazy, Suspense } from 'react';

const Reports = lazy(() => import('./Reports.jsx'));

class ErrorBoundary extends Component {
  state = { failed: false };

  static getDerivedStateFromError() {
    return { failed: true };
  }

  render() {
    if (this.state.failed) {
      return <p role="alert">Could not load reports.</p>;
    }
    return this.props.children;
  }
}

export default function App() {
  return (
    <ErrorBoundary>
      <Suspense fallback={<p>Loading reports…</p>}>
        <Reports />
      </Suspense>
    </ErrorBoundary>
  );
}

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. Lazy, at module levelconst Reports = lazy(() => import('./Reports.jsx'));

    import() ကို Reports ကို ပထမဆုံး render လုပ်ချိန်မှ ခေါ်ပါတယ်။ Reports.jsx ဖိုင်က default export တစ်ခု ရှိရပါမယ်။

  2. Switch to the fallbackstatic getDerivedStateFromError() {

    အောက်က component တစ်ခုခု throw လုပ်ရင် React က ဒီ static method ကို ခေါ်ပြီး ပြန်ပေးတဲ့ { failed: true } ကို state ထဲ ပေါင်းထည့်ပါတယ်။

  3. Render the alertreturn <p role="alert">Could not load reports.</p>;

    role="alert" ကြောင့် screen reader တွေက message ကို ချက်ချင်း ဖတ်ပြပါတယ်။ Error မရှိရင် this.props.children ကို ပုံမှန်အတိုင်း ပြပါတယ်။

  4. Loading boundary inside the error boundary<Suspense fallback={<p>Loading reports…</p>}>

    Reports suspend ဖြစ်နေစဉ် ဒီ fallback ပေါ်ပါတယ်။ ErrorBoundary က Suspense ရဲ့ အပြင်ဘက်မှာ ရှိလို့ import မအောင်မြင်ရင် error က boundary ဆီ ရောက်ပါတယ်။

You should see
ပထမဆုံး 'Loading reports…' ကို ပြပါတယ်။ Reports chunk ရောက်တာနဲ့ Reports component က fallback နေရာကို အစားထိုးပါတယ်။ Chunk download မအောင်မြင်ရင် ဒါမှမဟုတ် Reports render လုပ်ရင်း throw လုပ်ရင် 'Could not load reports.' alert ကို ပြပါတယ်။

Boundary တွေကို ဘယ်နေရာ ထားမလဲ

App တစ်ခုလုံးကို boundary တစ်ခုတည်းနဲ့ ထုပ်ရင် ဘယ် widget crash ဖြစ်ဖြစ် screen တစ်ခုလုံး error page ဖြစ်သွားပါတယ်။ Widget တိုင်းကို သီးခြား boundary နဲ့ ထုပ်ရင်တော့ loading spinner တွေ အများကြီး တစ်ခုပြီးတစ်ခု ပေါ်ပြီး screen က ခုန်ပေါက်နေပါတယ်။ လက်တွေ့မှာ အလယ်အလတ်ကို ရွေးပါတယ်: app ရဲ့ အပြင်ဆုံးမှာ 'Something went wrong' boundary တစ်ခု၊ route/page တစ်ခုစီမှာ တစ်ခု၊ ပြီးတော့ third-party widget လို ပျက်လွယ်တဲ့ အပိုင်းတွေမှာ သီးသန့် တစ်ခုစီ ထားပါတယ်။

lazy က မအောင်မြင်တဲ့ import ကို မှတ်ထားပါတယ်

lazy() က import() ရဲ့ ရလဒ်ကို cache လုပ်ပါတယ်၊ မအောင်မြင်တဲ့ ရလဒ်ကိုလည်း ပါပါတယ်။ ဒါကြောင့် boundary ကို reset လုပ်ပြီး lazy component တူတူကို ပြန် render လုပ်ရင် download ကို ထပ်မကြိုးစားဘဲ error တူတူ ပြန်ရပါတယ်။ Deploy အသစ်ကြောင့် chunk ပျောက်တာမျိုးအတွက် fallback ထဲမှာ 'Reload page' button (window.location.reload()) ပေးတာက အရိုးရှင်းဆုံး ဖြေရှင်းနည်းပါ။

လက်တွေ့ project တွေမှာ

လက်တွေ့ project တွေမှာ Suspense နဲ့ error boundary

Production React app တွေမှာ ဒီ tool နှစ်ခုကို performance နဲ့ reliability အတွက် အမြဲလိုလို တွဲသုံးပါတယ်။ User က crash ဖြစ်တာကို ရှောင်လို့ မရပေမဲ့ crash ဖြစ်တဲ့အခါ ဘာမြင်ရမလဲဆိုတာကို ထိန်းချုပ်လို့ ရပါတယ်။

  • Route-based code splitting: page တစ်ခုစီကို lazy() နဲ့ ခွဲထားလို့ home page က admin panel ရဲ့ code ကို download မလုပ်ရပါဘူး၊ ဒါက code splitting ကို စတင်ဖို့ အကောင်းဆုံး နေရာပါ။
  • Heavy widget တွေ: chart, rich-text editor, map လို library ကြီးတွေကို သုံးတဲ့ component တွေကို lazy လုပ်ပြီး skeleton UI ကို fallback အဖြစ် ပြပါတယ်။
  • Error monitoring: componentDidCatch ထဲကနေ Sentry လို service ဆီ error နဲ့ info.componentStack ကို ပို့ပြီး ဘယ် component မှာ crash ဖြစ်လဲ ခြေရာခံပါတယ်။
  • react-error-boundary library: class ကိုယ်တိုင် မရေးဘဲ FallbackComponent, onReset, resetKeys လို props တွေနဲ့ reset လုပ်လို့ရတဲ့ boundary ကို သုံးပါတယ်။

အမှားများ

Suspense နဲ့ error boundary မှာ ဖြစ်လေ့ရှိတဲ့ အမှားများ

Event handler error ကို boundary က ဖမ်းမယ်လို့ ထင်ခြင်း

onClick ထဲက throw လုပ်တဲ့ error က render အတွင်း မဟုတ်လို့ boundary က မဖမ်းပါဘူး၊ fallback လည်း မပေါ်ပါဘူး။ Handler ထဲမှာ try/catch နဲ့ ဖမ်းပြီး error message ကို state ထဲ ထည့်ကာ ပြပါ။ setTimeout ဒါမှမဟုတ် fetch ရဲ့ .then ထဲက error တွေအတွက်လည်း အတူတူပါပဲ။

lazy() ကို component ထဲမှာ ကြေညာခြင်း

function Page() { const Chart = lazy(...); ... } လို ရေးရင် render တိုင်း component type အသစ် ဖြစ်လို့ Chart ရဲ့ state အားလုံး ပျောက်ပြီး ပြန် suspend ဖြစ်ကာ fallback ကို ထပ်ခါထပ်ခါ ပြပါတယ်။ lazy ကို module ရဲ့ top level မှာ ကြေညာပါ။

Boundary တစ်ခုတည်းနဲ့ app တစ်ခုလုံး ထုပ်ခြင်း ဒါမှမဟုတ် reset မပေးခြင်း

အပြင်ဆုံး boundary တစ်ခုတည်း ရှိရင် sidebar widget သေးသေးလေး crash ဖြစ်တာနဲ့ app တစ်ခုလုံး error page ဖြစ်သွားပါတယ်။ Boundary ရဲ့ error state က အလိုလို မပျောက်ပါဘူး — data ပြင်ပြီးတာတောင် fallback ဆက်ပေါ်နေတဲ့အတွက် 'Try again' button နဲ့ state ကို reset လုပ်ပေးပါ၊ ဒါမှမဟုတ် key ပြောင်းပြီး boundary ကို ပြန်စပါ။

Mini challenge

Mini challenge: reset လုပ်လို့ရတဲ့ error boundary

Price မှားနေရင် throw လုပ်တဲ့ PriceWidget ကို error boundary နဲ့ ထုပ်ပါ။ Boundary က widget နာမည်နဲ့ error message ကို ပြပြီး 'Try again' button ပါရမယ်၊ componentDidCatch မှာ error ကို log လုပ်ရမယ်။ Boundary အပြင်ဘက်က 'Fix data' button နဲ့ price ကို ပြင်ပြီး Try again နှိပ်မှ widget ပြန်ပေါ်ရပါမယ်။

  • getDerivedStateFromError က error object ကို state ထဲ သိမ်းရမယ်
  • componentDidCatch က 'logged: <message>' ကို console ထဲ ထုတ်ရမယ်
  • Fallback က role="alert" ပါပြီး '<title> failed: <message>' နဲ့ Try again button ပြရမယ်
  • Try again က error state ကို null ပြန်ထားရမယ်၊ boundary အပြင်ဘက်က UI က crash ချိန်မှာလည်း ဆက်ပေါ်ရမယ်
Dashboard.jsx
import { Component, useState } from 'react';

class ErrorBoundary extends Component {
  // TODO: state = { error: null }
  // TODO: static getDerivedStateFromError(error) -> put the error in state
  // TODO: componentDidCatch(error) -> console.log a "logged: ..." line
  // TODO: reset method that clears the error
  render() {
    // TODO: alert with "<title> failed: <message>" and a Try again button
    return this.props.children;
  }
}

function PriceWidget({ price }) {
  // TODO: throw new Error('price cannot be negative') when price < 0
  return <p>Price: {price} USD</p>;
}

export default function Dashboard() {
  // TODO: price state starting at -5, a boundary around PriceWidget,
  // TODO: and a "Fix data" button outside the boundary that sets the price to 25
  return null;
}

ရလဒ်: ပထမ render မှာ 'Price widget failed: price cannot be negative' alert နဲ့ Try again button ပေါ်ပြီး console မှာ 'logged: price cannot be negative' ထွက်ပါတယ်။ 'The rest of the dashboard still works.' စာကြောင်းနဲ့ Fix data button ကတော့ ဆက်ပေါ်နေပါတယ်။ Fix data နှိပ်ရင် alert ဆက်ရှိနေပြီး Try again နှိပ်မှ 'Price: 25 USD' ပေါ်လာပါတယ်။

အရိပ်အမြွက် ကြည့်ရန်

reset = () => this.setState({ error: null }) လို arrow function class field နဲ့ ရေးရင် onClick={this.reset} မှာ this ပျောက်မသွားပါဘူး။ Fix data နှိပ်ရုံနဲ့ widget ပြန်မပေါ်တာ မှန်ပါတယ် — boundary ရဲ့ error state ကို ကိုယ်တိုင် reset လုပ်ရပါမယ်။

အဖြေ ကြည့်ရန်
Dashboard.jsx
import { Component, useState } from 'react';

class ErrorBoundary extends Component {
  state = { error: null };

  static getDerivedStateFromError(error) {
    return { error }; // show the fallback on the next render
  }

  componentDidCatch(error) {
    console.log(`logged: ${error.message}`); // e.g. send to a monitoring service
  }

  reset = () => this.setState({ error: null });

  render() {
    if (this.state.error) {
      return (
        <div role="alert">
          <p>{this.props.title} failed: {this.state.error.message}</p>
          <button onClick={this.reset}>Try again</button>
        </div>
      );
    }
    return this.props.children;
  }
}

function PriceWidget({ price }) {
  if (price < 0) throw new Error('price cannot be negative');
  return <p>Price: {price} USD</p>;
}

export default function Dashboard() {
  const [price, setPrice] = useState(-5);
  return (
    <>
      <ErrorBoundary title="Price widget">
        <PriceWidget price={price} />
      </ErrorBoundary>
      <button onClick={() => setPrice(25)}>Fix data</button>
      <p>The rest of the dashboard still works.</p>
    </>
  );
}

Quick Quiz 1 / 3

Worked example မှာ network ပြတ်နေလို့ Reports chunk download မအောင်မြင်ဘူး။ User က ဘာမြင်ရမလဲ?

Quick Quiz 2 / 3

ဘယ် error ကို error boundary က မဖမ်းနိုင်ဘူးလဲ?

Quick Quiz 3 / 3

ဒီ code မှာ ဘာပြဿနာ ရှိလဲ? function Page() { const Chart = lazy(() => import('./Chart.jsx')); return <Suspense fallback={<Spinner />}><Chart /></Suspense>; }

မှတ်ထားရမယ့် အချက်များ

  • lazy(() => import(...)) က component code ကို ပထမဆုံး လိုတဲ့အချိန်မှ download လုပ်ပြီး module top level မှာ ကြေညာရပါတယ်
  • Suspend ဖြစ်တဲ့ component အတွက် အနီးဆုံး <Suspense> က fallback ကို ပြပါတယ်၊ useEffect fetch ကတော့ Suspense ကို မထိပါဘူး
  • Error boundary က class component (ဒါမှမဟုတ် react-error-boundary လို library) ဖြစ်ရပြီး render, effect, lazy import error တွေကို ဖမ်းပါတယ်
  • Event handler နဲ့ async callback error တွေကို boundary မဖမ်းလို့ try/catch သုံးပါ
  • Boundary တွေကို app, page, ပျက်လွယ်တဲ့ widget အဆင့်တွေမှာ ထားပြီး reset လုပ်နည်း ပေးပါ

Suspense — React

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Suspense, Lazy Loading & Error Boundaries | Thuta Learning