0Pricing
Next.js 15 Fullstack (App Router + Server Actions) · 课时

加载与错误用户界面

使用 `loading.js` 实现加载状态,并通过应用中的 `error.js` 边界妥善处理错误

加载与错误用户界面 是 CoddyKit 上的免费 Next.js 15 Fullstack (App Router + Server Actions) 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Next.js 15 Fullstack (App Router + Server Actions) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Next.js 15 Fullstack (App Router + Server Actions) 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Why Loading States Matter

When your Next.js application fetches data, it can take some time. Without any visual feedback, users might think the app is frozen or broken.

  • Improve User Experience: Keep users informed.
  • Reduce Frustration: Prevent perceived slowness.
  • Indicate Progress: Show that something is happening in the background.

Introducing `loading.js`

Next.js App Router provides a special file convention, loading.js, to automatically create a loading UI for a route segment.

When data is being fetched for a page.js or layout.js file, Next.js automatically displays the UI defined in its nearest loading.js sibling or ancestor.

Creating a Simple Loading UI

To create a loading UI, simply add a loading.js file inside a route segment folder. It should export a default React component.

This component will be rendered instantly when the segment is loading.

export default function Loading() {
  return (
    <div style={{ padding: '20px', textAlign: 'center' }}>
      <h2>Loading...</h2>
      <p>Please wait while we fetch the content.</p>
    </div>
  );
}

How `loading.js` Works

Under the hood, loading.js works with React Suspense. Next.js wraps the corresponding page.js and its nested children in a Suspense Boundary.

When any data fetching inside that boundary is pending, the loading.js component is shown as a fallback.

Nested Loading States

You can have multiple loading.js files at different levels of your route. For example, a global loading UI and a more specific one for a sub-route.

Next.js will show the closest ancestor loading.js for the segment that is currently loading.

Handling Application Errors

Just like loading states, errors are a part of any application. Uncaught errors can crash your app, leading to a poor user experience.

Next.js App Router provides a way to gracefully handle errors and display a custom error UI using error.js.

Introducing `error.js`

The error.js file convention allows you to define an error UI that automatically wraps a route segment and its children in a React Error Boundary.

This boundary catches errors that occur during rendering, in event handlers, and in data fetching within its scope.

Creating a Basic Error UI

Similar to loading.js, create an error.js file in a route segment. It must be a React Client Component (use 'use client'; at the top).

It receives error (the error object) and a reset function as props.

'use client';

import { useEffect } from 'react';

export default function Error({ error, reset }) {
  useEffect(() => {
    // Log the error to an error reporting service
    console.error(error);
  }, [error]);

  return (
    <div style={{ padding: '20px', textAlign: 'center', color: 'red' }}>
      <h2>Something went wrong!</h2>
      <button
        onClick={() => reset()} // Attempt to re-render the segment
        style={{ marginTop: '10px', padding: '8px 15px' }}
      >
        Try again
      </button>
    </div>
  );
}

Error Recovery with `reset()`

The reset() function provided to your error.js component is crucial. When called, it attempts to re-render the entire segment that threw the error.

This allows users to try again without navigating away, potentially resolving transient issues.

Quick Check on UI Files

Which statement accurately describes the primary purpose of loading.js in Next.js App Router?

Recap: Loading & Error UI

In this lesson, you learned how to enhance your Next.js application's user experience by implementing visual feedback for loading and error states.

  • loading.js: Displays a temporary UI while data is being fetched, using React Suspense.
  • error.js: Catches errors in a segment and displays a fallback UI, allowing users to attempt recovery with the reset() function.

These conventions help create more robust and user-friendly Next.js applications.

常见问题解答

「加载与错误用户界面」课时是免费的吗?

是的 — 「加载与错误用户界面」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Next.js 15 Fullstack (App Router + Server Actions) 课程的其余内容,请升级到 CoddyKit PRO。 Next.js 15 Fullstack (App Router + Server Actions) 课程共包含 4 节课。

「加载与错误用户界面」这节课中我会学到什么?

使用 `loading.js` 实现加载状态,并通过应用中的 `error.js` 边界妥善处理错误 你通过在浏览器中直接运行的动手代码来练习 Next.js 15 Fullstack (App Router + Server Actions),全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Next.js 15 Fullstack (App Router + Server Actions) 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Next.js 15 Fullstack (App Router + Server Actions) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。

「加载与错误用户界面」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Next.js 15 Fullstack (App Router + Server Actions) 课中编写并运行代码吗?

能。每节 Next.js 15 Fullstack (App Router + Server Actions) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 布局与页面组件
  2. 动态路由与参数
  3. 加载与错误用户界面
  4. 路由组与嵌套路由布局
← 返回 Next.js 15 Fullstack (App Router + Server Actions)