Interfaces de chargement et d’erreur
Implémentez des états de chargement avec `loading.js` et gérez élégamment les erreurs grâce aux limites `error.js` dans votre application.
Interfaces de chargement et d’erreur est une leçon Next.js 15 Fullstack (App Router + Server Actions) gratuite sur CoddyKit. Ceci est la leçon 3 sur 4. Tu peux lire la leçon complète ci-dessous gratuitement — puis la pratiquer en direct dans le navigateur avec un éditeur de code intégré et un tuteur IA 24/7. Elle fait partie du parcours d'apprentissage Next.js 15 Fullstack (App Router + Server Actions), et ta progression se synchronise sur le web et l'application CoddyKit. Le cours Next.js 15 Fullstack (App Router + Server Actions) comprend 4 leçons au total.
Certaines parties de cette leçon n'ont pas encore été traduites et s'affichent en anglais.
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 thereset()function.
These conventions help create more robust and user-friendly Next.js applications.
Questions Fréquemment Posées
La leçon « Interfaces de chargement et d’erreur » est-elle gratuite ?
Oui — le texte complet de « Interfaces de chargement et d’erreur » est gratuit à lire ici sur le web. Pour la pratiquer de manière interactive (un éditeur de code intégré et un tuteur IA 24/7) et déverrouiller le reste du cours Next.js 15 Fullstack (App Router + Server Actions), passe à CoddyKit PRO. Le cours Next.js 15 Fullstack (App Router + Server Actions) comprend 4 leçons au total.
Qu'est-ce que j'apprendrai dans « Interfaces de chargement et d’erreur » ?
Implémentez des états de chargement avec `loading.js` et gérez élégamment les erreurs grâce aux limites `error.js` dans votre application. Tu pratiques Next.js 15 Fullstack (App Router + Server Actions) avec du code pratique que tu exécutes directement dans le navigateur, et un tuteur IA 24/7 répond à tes questions au fur et à mesure que tu avances dans la leçon.
Dois-je avoir de l'expérience pour commencer Next.js 15 Fullstack (App Router + Server Actions) ?
Aucune expérience préalable n'est requise. Next.js 15 Fullstack (App Router + Server Actions) sur CoddyKit est structuré pour les débutants jusqu'aux apprenants avancés, donc tu peux commencer ici ou depuis le début et avancer à ton rythme. Ceci est la leçon 3 sur 4.
Combien de temps prend la leçon « Interfaces de chargement et d’erreur » ?
La plupart des leçons CoddyKit prennent environ 5–10 minutes. Chacune est courte et interactive, tu progresses régulièrement et tu repiques exactement où tu t'es arrêté sur le web et l'app.
Peux-tu écrire et exécuter du code dans cette leçon Next.js 15 Fullstack (App Router + Server Actions) ?
Oui. Chaque leçon Next.js 15 Fullstack (App Router + Server Actions) inclut un éditeur de code intégré, tu écris et exécutes du vrai code directement dans ton navigateur et tu reçois des retours IA instantanés — aucune configuration locale requise.
Toutes les leçons de ce cours
- Mises en page et composants de page
- Routes dynamiques et paramètres
- Interfaces de chargement et d’erreur
- Groupes de routes et mises en page imbriquées