0
0
NextJSframework~20 mins

Build output analysis in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Next.js Build Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What will be the output of this Next.js Server Component?

Consider this Next.js Server Component code snippet. What will it render on the page?

NextJS
export default function Greeting() {
  const hour = new Date().getHours();
  return (
    <main>
      <h1>{hour < 12 ? 'Good morning' : 'Good afternoon'}</h1>
    </main>
  );
}
A<main><h1>Good evening</h1></main> regardless of the time
B<main><h1>Good night</h1></main> regardless of the time
CAn error because Server Components cannot use Date()
D<main><h1>Good morning</h1></main> if current hour is before 12, otherwise <main><h1>Good afternoon</h1></main>
Attempts:
2 left
💡 Hint

Think about when Server Components run and what JavaScript features they can use.

state_output
intermediate
2:00remaining
What is the rendered output after clicking the button twice in this React component?

Given this React functional component using hooks, what will be the displayed count after clicking the button two times?

NextJS
import { useState } from 'react';

export default function Counter() {
  const [count, setCount] = useState(0);

  function handleClick() {
    setCount(count + 1);
    setCount(count + 1);
  }

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={handleClick}>Increment Twice</button>
    </div>
  );
}
ACount: 0
BCount: 1
CCount: 2
DCount: NaN
Attempts:
2 left
💡 Hint

Remember how React batches state updates and how the current state value is captured in closures.

📝 Syntax
advanced
2:00remaining
Which option correctly imports and uses a Next.js dynamic component with suspense?

Next.js supports dynamic imports with React suspense. Which code snippet correctly implements this?

A
import dynamic from 'next/dynamic';
const DynamicComp = dynamic(() =&gt; import('./Comp'), { suspense: true });
export default function Page() {
  return &lt;React.Suspense fallback={&lt;p&gt;Loading...&lt;/p&gt;}&gt;&lt;DynamicComp /&gt;&lt;/React.Suspense&gt;;
}
B
import dynamic from 'next/dynamic';
const DynamicComp = dynamic(() =&gt; import('./Comp'), { loading: () =&gt; &lt;p&gt;Loading...&lt;/p&gt; });
export default function Page() {
  return &lt;DynamicComp /&gt;;
}
C
import dynamic from 'next/dynamic';
const DynamicComp = dynamic(() =&gt; import('./Comp'), { suspense: false });
export default function Page() {
  return &lt;DynamicComp /&gt;;
}
D
import dynamic from 'next/dynamic';
const DynamicComp = dynamic(() =&gt; import('./Comp'));
export default function Page() {
  return &lt;React.Suspense fallback={&lt;p&gt;Loading...&lt;/p&gt;}&gt;&lt;DynamicComp /&gt;&lt;/React.Suspense&gt;;
}
Attempts:
2 left
💡 Hint

Check how suspense option and React.Suspense wrapper are used together.

🔧 Debug
advanced
2:00remaining
Why does this Next.js page fail to build with this error: "Error: You should not use 'use client' in a Server Component"?

Examine the code below. Why does the build fail with the given error?

NextJS
'use client';

import React from 'react';

export default function Page() {
  return <h1>Hello from Server Component</h1>;
}
ABecause the component returns plain HTML instead of JSX
BBecause 'use client' is missing and the component uses client hooks
CBecause 'use client' directive is present but the component does not use any client-only features
DBecause the component imports React but does not export default
Attempts:
2 left
💡 Hint

Think about what 'use client' means and when it should be used.

🧠 Conceptual
expert
2:00remaining
What is the primary reason Next.js splits code into separate chunks during build?

Why does Next.js create multiple JavaScript chunks instead of one big file in the build output?

ATo improve page load performance by loading only necessary code per page
BTo make the build process faster by splitting files arbitrarily
CTo reduce server memory usage by limiting file sizes
DTo avoid using React features that require a single bundle
Attempts:
2 left
💡 Hint

Think about user experience and network efficiency.