0
0
NextJSframework~20 mins

App directory (App Router) in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
App Router Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the rendered output of this Next.js App Router component?

Consider this React Server Component in the app/page.tsx file:

export default function Page() {
  return (
    <main>
      <h1>Welcome to Next.js App Router</h1>
      <p>This is the home page.</p>
    </main>
  )
}

What will the browser display when visiting the root URL?

NextJS
export default function Page() {
  return (
    <main>
      <h1>Welcome to Next.js App Router</h1>
      <p>This is the home page.</p>
    </main>
  )
}
AA page with a heading 'Welcome to Next.js App Router' and a paragraph 'This is the home page.'
BA blank page with no content
CAn error page because the component is missing a layout
DA page showing only the paragraph 'This is the home page.' without the heading
Attempts:
2 left
💡 Hint

Think about what the component returns inside the main tag.

📝 Syntax
intermediate
2:00remaining
Which option correctly defines a layout in the Next.js App Router?

In the Next.js App Router, you want to create a layout that wraps all pages in a folder. Which of these code snippets correctly defines a layout component?

A
export default function Layout({ children }) {
  return children
}
B
export default function Layout() {
  return &lt;div&gt;No children here&lt;/div&gt;
}
C
export default function Layout(children) {
  return &lt;div&gt;{children}&lt;/div&gt;
}
D
export default function Layout({ children }) {
  return &lt;div&gt;{children}&lt;/div&gt;
}
Attempts:
2 left
💡 Hint

Layouts receive a children prop as an object property.

lifecycle
advanced
2:00remaining
When does a React Server Component in Next.js App Router re-run on the server?

Given a React Server Component in the Next.js App Router, under which condition will it re-run on the server to fetch fresh data?

AOnly once at build time and never again
BWhen the user navigates to the page or refreshes the browser
CEvery time the user clicks a button inside the component
DOnly when the component's client-side state changes
Attempts:
2 left
💡 Hint

Think about when server components run in Next.js App Router.

🔧 Debug
advanced
2:00remaining
Why does this Next.js App Router page cause a runtime error?

Examine this app/page.tsx code:

export default function Page() {
  const data = fetch('https://api.example.com/data')
  return (
    <div>{JSON.stringify(data)}</div>
  )
}

What is the cause of the runtime error?

NextJS
export default async function Page() {
  const data = await fetch('https://api.example.com/data')
  return (
    <div>{JSON.stringify(data)}</div>
  )
}
Afetch returns a Promise, but the code tries to use it synchronously without awaiting
Bfetch is not allowed in server components
CJSON.stringify cannot convert objects returned by fetch
DThe component is missing a useEffect hook to fetch data
Attempts:
2 left
💡 Hint

Remember that fetch returns a Promise and needs to be awaited.

state_output
expert
2:00remaining
What is the output after clicking the button in this Next.js App Router client component?

Consider this client component in app/counter/page.tsx:

'use client'
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>
  )
}

What will the count display after clicking the button once?

NextJS
'use client'
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: NaN
BCount: 0
CCount: 1
DCount: 2
Attempts:
2 left
💡 Hint

Remember how React batches state updates with the same value.