# The loading.js Convention

> Instant loading states powered by React Suspense.

Subject: Next.js | Lesson 9 of 65 | 4 min read
Interactive lesson: https://codewithanimation.in/nextjs/loading-file
Previous: [The template.js Convention](https://codewithanimation.in/nextjs/template-file.md) | Next: [The error.js Convention](https://codewithanimation.in/nextjs/error-file.md)

## Instant fallbacks

The `loading.js` file allows you to create fallback UI—like a spinner, a progress bar, or a skeleton screen—that is shown to the user immediately upon navigation.

As soon as the user clicks a link, Next.js instantly renders this fallback UI while the actual page content is being generated (usually waiting on an `async` database query on the server).

## Perceived performance

Without a loading state, a user clicking a link might experience a frozen screen for several seconds while the server fetches data. They might click the button multiple times, thinking the app is broken.

`loading.js` completely solves this by providing immediate visual feedback. The transition happens instantly, vastly improving the perceived performance of your application.

## Automatic Suspense

To use it, just export a default React component from a `loading.tsx` file.

Under the hood, Next.js takes your `loading.js` component and uses it as the `fallback` prop in a React `<Suspense>` boundary. It automatically wraps this boundary around your `page.js`.

## Common mistake: Interactive layouts

A common misconception is that a loading state blocks the entire screen.

Because `loading.js` only wraps the specific route segment it sits next to, the surrounding `layout.js` (like your navigation sidebar) renders instantly and remains completely interactive. A user can actually click a different link while the current page is still loading!

## Tip: Skeleton screens

While a simple spinner works, the best user experience comes from 'Skeleton UI'.

Design your `loading.js` to look exactly like your `page.js`, but with gray pulsing boxes instead of real text and images. This prevents jarring layout shifts when the real data finally arrives.

## Try it: Simulate Slow Network

Click the button to simulate a slow database query. Watch how the layout renders instantly, the skeleton placeholder kicks in, and the UI remains interactive.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/nextjs/loading-file

## Check yourself

### Question 1

Next.js implements the loading.js file convention using which underlying React feature?

- A. React Context
- B. React Suspense
- C. React Portals
- D. useEffect

Correct answer: B

Next.js automatically wraps your route segment in a `<Suspense>` boundary, using your `loading.js` component as the `fallback` UI.

- Why not A: Context is for sharing state.
- Why not C: Portals are for rendering outside the DOM hierarchy (like modals).
- Why not D: loading.js works seamlessly on the server without client-side effects.

### Question 2

While the loading.js fallback is visible, what is the state of the surrounding layout.js (e.g., the sidebar)?

- A. It is hidden until the page loads
- B. It is visible but completely frozen/unclickable
- C. It is visible and fully interactive
- D. It is replaced by a global loading screen

Correct answer: C

React Suspense is non-blocking. The layout renders immediately and remains fully interactive, allowing the user to navigate elsewhere even if the current page is still loading.

- Why not A: The layout renders instantly.
- Why not B: The layout remains fully interactive.
- Why not D: loading.js only affects its specific route segment.

## Remember this

- `loading.js` creates an instant loading UI for a route segment.
- It uses React `<Suspense>` under the hood.
- The surrounding layout remains fully interactive while the inner page is loading.
- Skeleton screens provide the best UX to prevent layout shift.
