0
0
NextJSframework~20 mins

Loading UI with loading.tsx in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Loading UI Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this loading.tsx component display during page load?

Consider this loading.tsx file in a Next.js app directory. What will the user see while the page is loading?

NextJS
export default function Loading() {
  return <div role="status" aria-live="polite">Loading content, please wait...</div>;
}
AA visible message: 'Loading content, please wait...' with screen reader announcement.
BNothing is displayed because the component returns null.
CAn error occurs because the component lacks a React import.
DA spinner icon is shown automatically by Next.js without custom code.
Attempts:
2 left
💡 Hint

Check what the component returns and the role attribute used.

📝 Syntax
intermediate
2:00remaining
Which loading.tsx code snippet correctly defines a loading component in Next.js 14+?

Choose the correct syntax for a loading component in Next.js 14+ using TypeScript.

A
export default function Loading() {
  return &lt;p&gt;Loading...&lt;/p&gt;;
}
B
export const Loading = () =&gt; {
  return &lt;p&gt;Loading...&lt;/p&gt;;
};
C
function Loading() {
  return &lt;p&gt;Loading...&lt;/p&gt;;
}
export default Loading;
Dexport default Loading = () => <p>Loading...</p>;
Attempts:
2 left
💡 Hint

Next.js expects a default export function named Loading.

state_output
advanced
2:30remaining
What happens if loading.tsx uses useState inside the loading component?

Given this loading.tsx code, what is the output or behavior during loading?

import { useState, useEffect } from 'react';

'use client';

export default function Loading() {
  const [dots, setDots] = useState('');

  useEffect(() => {
    const interval = setInterval(() => {
      setDots(d => (d.length < 3 ? d + '.' : ''));
    }, 500);
    return () => clearInterval(interval);
  }, []);

  return 
Loading{dots}
; }
AThe dots appear instantly and do not animate.
BThe component throws an error because hooks cannot be used in loading.tsx.
CThe loading message cycles through 'Loading', 'Loading.', 'Loading..', 'Loading...' every 500ms.
DThe loading message stays static as 'Loading' without dots.
Attempts:
2 left
💡 Hint

Think about how useState and useEffect work together to update UI over time.

🔧 Debug
advanced
2:30remaining
Why does this loading.tsx cause a hydration mismatch error?

Examine this loading.tsx code snippet. Why does Next.js show a hydration mismatch warning?

'use client';

export default function Loading() {
  const date = new Date();
  return 
Loading at {date.toLocaleTimeString()}
; }
ABecause the loading component must return null, not JSX.
BBecause the loading component renders different content on server and client due to dynamic date/time.
CBecause the component is missing a key prop on the div element.
DBecause the date object is not serializable and causes a runtime error.
Attempts:
2 left
💡 Hint

Consider what happens when server and client render different outputs.

🧠 Conceptual
expert
3:00remaining
What is the main purpose of loading.tsx in Next.js 14+ app router?

Choose the best explanation for why Next.js uses a loading.tsx file in the app directory.

ATo preload all data before rendering the page, blocking UI until ready.
BTo replace the default 404 page with a custom loading spinner.
CTo handle errors during page rendering and display fallback UI.
DTo show a temporary UI while a route or segment is loading asynchronously, improving user experience.
Attempts:
2 left
💡 Hint

Think about what users see when navigating between pages that take time to load.