-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloading-error-boundaries-in-app-dir.js
More file actions
87 lines (74 loc) · 2.12 KB
/
loading-error-boundaries-in-app-dir.js
File metadata and controls
87 lines (74 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
* 📘 Topic: Loading & Error Boundaries in Next.js App Directory
*
* Next.js App Directory (`app/`) uses React Suspense by default.
* It supports:
* - loading.js → shown during route or component load
* - error.js → shown when component throws error
*
* ✅ Improves UX via skeletons/loaders
* ✅ Provides scoped error handling
*/
/**
* 📁 File structure:
* /app
* └── dashboard
* ├── page.js → main route component
* ├── loading.js → shown while page.js is loading
* └── error.js → shown if page.js throws error
*/
// 🔹 page.js (can be async for data fetching)
export default async function DashboardPage() {
const res = await fetch("https://api.example.com/data", {
cache: "no-store",
});
if (!res.ok) {
// throw error to trigger error.js
throw new Error("Failed to fetch dashboard data");
}
const data = await res.json();
return (
<div>
<h1>Dashboard</h1>
<pre>{JSON.stringify(data, null, 2)}</pre>
</div>
);
}
// 🔹 loading.js (auto shown during `page.js` load)
export default function Loading() {
return (
<div style={{ padding: "20px" }}>
<h2>Loading dashboard...</h2>
</div>
);
}
// 🔹 error.js (auto shown if `page.js` throws)
'use client'; // required
import { useEffect } from "react";
export default function Error({ error, reset }) {
useEffect(() => {
console.error("Dashboard error:", error);
}, [error]);
return (
<div>
<h2>Something went wrong!</h2>
<p>{error.message}</p>
<button onClick={() => reset()}>Try again</button>
</div>
);
}
/**
* 🔍 Key Notes:
* - loading.js auto-wraps your page in <Suspense />
* - error.js is a React error boundary (auto-catch on page-level)
* - reset() allows retrying route render
*
* 🧠 Tip: You can define loading/error per segment (page, layout, etc.)
* - Also works with nested routing, so each segment can show its own fallback!
*/
/**
* 🔥 Real-world uses:
* - Show spinner/skeleton on data fetch
* - Display fallback UI if API fails
* - Retry mechanism on errors
*/