Loading UI shows users that something is happening while the page or data is loading. It helps keep users informed and improves experience.
Loading UI with loading.tsx in NextJS
export default function Loading() { return ( <div role="status" aria-live="polite"> <p>Loading...</p> </div> ) }
role="status" and aria-live="polite" for accessibility.export default function Loading() { return <p>Loading...</p> }
export default function Loading() { return ( <div role="status" aria-live="polite"> <p>Loading your content, please wait.</p> </div> ) }
export default function Loading() { return ( <div role="status" aria-live="polite" style={{ color: 'blue', fontWeight: 'bold' }}> <p>Loading...</p> </div> ) }
This is a complete loading.tsx component for Next.js. It shows a centered loading message with accessibility roles. Next.js will automatically show this UI when a page or segment is loading.
import React from 'react' export default function Loading() { return ( <div role="status" aria-live="polite" style={{ padding: '1rem', textAlign: 'center' }}> <p>Loading...</p> </div> ) }
Place loading.tsx inside the folder of the page or layout where you want the loading UI to appear.
Next.js automatically shows this component during loading states without extra code.
Use simple text and accessibility roles so screen readers announce the loading status.
Loading UI improves user experience by showing progress during waits.
Next.js uses loading.tsx files to display loading states automatically.
Always use semantic HTML and ARIA roles for accessibility in loading components.
Practice
loading.tsx file in a Next.js app?Solution
Step 1: Understand the role of loading.tsx in Next.js
Theloading.tsxfile is used to display a loading UI automatically during page or component loading.Step 2: Differentiate from other files
It is not for layout, API handling, or styling, which are handled by other files.Final Answer:
To show a loading indicator automatically while a page or component is loading -> Option DQuick Check:
loading.tsx shows loading UI = A [OK]
- Confusing loading.tsx with layout.tsx
- Thinking loading.tsx handles API calls
- Assuming loading.tsx is for styling
loading.tsx for Next.js?Solution
Step 1: Check correct export syntax
In Next.js, the loading component should be exported as default usingexport default functionorexport default const.Step 2: Validate JSX return
The function must return JSX properly with parentheses or directly.Final Answer:
export default function Loading() { returnLoading...
; } -> Option CQuick Check:
Default export function with return JSX = D [OK]
- Forgetting default keyword in export
- Incorrect arrow function syntax without return
- Using invalid export syntax
loading.tsx component:export default function Loading() {
return Loading data...;
}What will the user see while the page is loading?
Solution
Step 1: Analyze the JSX output
The component returns adivwith text 'Loading data...' and accessibility roles.Step 2: Understand accessibility roles
Therole="status"andaria-live="polite"announce the text to screen readers politely.Final Answer:
A div with text 'Loading data...' announced politely by screen readers -> Option AQuick Check:
Accessible loading text shown = C [OK]
- Assuming no output without spinner
- Thinking aria-live causes errors
- Ignoring accessibility roles
loading.tsx file is not showing the loading UI as expected:export default function Loading() {
Loading...
}What is the problem?
Solution
Step 1: Check function syntax
The function body has JSX but noreturnstatement, so nothing is rendered.Step 2: Confirm React import is optional in Next.js 13+
React import is not required for JSX in Next.js 13+, so it's not the issue.Final Answer:
The function does not return the JSX element -> Option AQuick Check:
Missing return causes no output = B [OK]
- Forgetting to return JSX
- Thinking React import is mandatory
- Believing lowercase component names are valid
loading.tsx. Which code snippet correctly implements this?Solution
Step 1: Identify correct ARIA roles for loading UI
Therole="status"witharia-live="polite"announces updates politely without interrupting.Step 2: Check spinner accessibility
The spinner hasaria-hidden="true"so screen readers ignore it, focusing on text.Step 3: Confirm semantic structure
Thedivwraps spinner and text properly for screen readers and visual users.Final Answer:
export default function Loading() { return <div role="status" aria-live="polite"><span className="spinner" aria-hidden="true"></span><span>Please wait...</span></div>; } -> Option BQuick Check:
Use role=status + aria-live=polite + aria-hidden spinner = A [OK]
- Using role=alert which interrupts screen readers
- Missing aria-hidden on spinner
- Not using any ARIA roles for loading
Ready to prove it?
Pass the test and this pattern moves unknown
