Render Error ဖြစ်ရင် ပြန်ထိန်းပါ

19.3

UI ရဲ့ အပိုင်းတစ်ခုကို error boundary နဲ့ ခြံထားပါ။ Render ပျက်ရင် အဲဒီနေရာမှာ နားလည်လွယ်တဲ့ message ပြနိုင်ပါတယ်။

ဒီစာမျက်နှာမှာ

Fallback UI ပြပါ

Class boundary က child render error ကို fallback UI အဖြစ် ပြောင်းပြနိုင်ပါတယ်။ ဘာပြမလဲ ပြောင်းဖို့ getDerivedStateFromError သုံးပါ။ componentDidCatch ကနေ error နဲ့ component stack ကို logging service ဆီ ပို့နိုင်ပါတယ်။

import React, { Component } from "react";

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

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

  render() {
    return this.state.failed
      ? <p role="alert">This section could not load.</p>
      : this.props.children;
  }
}

export default SectionBoundary;

တခြား Error တွေကို ဖြစ်တဲ့နေရာမှာ ကိုင်တွယ်ပါ

fetch မအောင်တာနဲ့ event handler error တွေကို ဖြစ်တဲ့နေရာမှာ ကိုင်တွယ်ပါ။ Boundary က သူ့ကိုယ်ပိုင် error နဲ့ server rendering error ကို မဖမ်းပါဘူး။ Record ပြောင်းတိုင်း boundary အသစ်နဲ့ စဖို့ key ကို record ID အတိုင်း ပြောင်းပါ။

<SectionBoundary key={productId}>
  <StockPanel productId={productId} />
</SectionBoundary>

တရားဝင် ကိုးကားချက်များ