//App.tsx
const Heatmap = React.lazy(() =>
import("./Heatmap").then((d) => ({ default: d.Heatmap }))
);
...略
<React.Suspense fallback="loading...">
<main ...略
>
<Heatmap ref={innerRef} />
</main>
</React.Suspense>
const MarkdownPreview = lazy(() => delayForDemo(import('./MarkdownPreview.js')));
...
<Suspense fallback={<Loading />}>
<h2>Preview</h2>
<MarkdownPreview markdown={markdown} />
</Suspense>
import { ErrorBoundary, FallbackProps } from "react-error-boundary";
...
// 当出错时显示的内容
function ErrorFallback({ error }: FallbackProps) {
return (
<div role="alert" className="text-red-500 font-semibold">
<p>
Heatmap failed to render. Can you re-index your graph and try again?
</p>
</div>
);
}
...
<ErrorBoundary FallbackComponent={ErrorFallback}>
// 你的UI代码
<DateRange range={range} onRangeChange={setRange} today={today} />
{range && (
<HeatmapChart today={today} endDate={range[1]} startDate={range[0]} />
)}
</ErrorBoundary>