Bird
Raised Fist0
NextJSframework~10 mins

App directory (App Router) in NextJS - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a basic page component in the Next.js App Router.

NextJS
export default function Home() {
  return <[1]>Hello, Next.js App Router!</[1]>;
}
Drag options to blanks, or click blank then click option'
Adiv
Bspan
Csection
Dheader
Attempts:
3 left
💡 Hint
Common Mistakes
Using an inline element like span which is less common for main containers.
2fill in blank
medium

Complete the code to export a page component as a React Server Component in Next.js App Router.

NextJS
export default async function Page() {
  const data = await fetch('/api/data');
  const json = await data.json();
  return <pre>{JSON.stringify([1], null, 2)}</pre>;
}
Drag options to blanks, or click blank then click option'
Adata
Bjson
CPage
Dfetch
Attempts:
3 left
💡 Hint
Common Mistakes
Using the raw fetch response object instead of the parsed JSON.
3fill in blank
hard

Fix the error in the Next.js App Router page component by completing the import statement.

NextJS
import [1] from 'next/navigation';

export default function Page() {
  const router = useRouter();
  return <button onClick={() => router.push('/about')}>Go to About</button>;
}
Drag options to blanks, or click blank then click option'
AuseContext
BuseState
CuseEffect
DuseRouter
Attempts:
3 left
💡 Hint
Common Mistakes
Importing hooks unrelated to routing causes runtime errors.
4fill in blank
hard

Fill both blanks to create a layout component that wraps page content in Next.js App Router.

NextJS
export default function [1]({ children }) {
  return <[2]>{children}</[2]>;
}
Drag options to blanks, or click blank then click option'
ALayout
BPage
Cdiv
Dsection
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect component names or inline elements for wrapping.
5fill in blank
hard

Fill all three blanks to create a server action that updates data and redirects in Next.js App Router.

NextJS
import { redirect } from 'next/navigation';

export async function [1](formData) {
  await updateData(formData.get('[2]'));
  [3]('/success');
}
Drag options to blanks, or click blank then click option'
AupdateUser
Busername
Credirect
DhandleSubmit
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong function names or missing the redirect call.

Practice

(1/5)
1. In Next.js App Router, what does a folder containing a page.js file represent?
easy
A. A route in the application
B. A layout wrapper for pages
C. A server API endpoint
D. A static asset folder

Solution

  1. Step 1: Understand the role of page.js in App Router

    In Next.js App Router, each folder with a page.js file defines a route that users can visit.
  2. Step 2: Differentiate from other files

    Files like layout.js define layouts, not routes. API routes are in api/ folders, and static assets are in public/.
  3. Final Answer:

    A route in the application -> Option A
  4. Quick Check:

    Folder + page.js = route [OK]
Hint: Folder with page.js always means a route [OK]
Common Mistakes:
  • Confusing layout.js with page.js
  • Thinking page.js is for API routes
  • Assuming static assets are routes
2. Which of the following is the correct way to define a layout in the Next.js App Router?
easy
A. Create a layout.js file inside the route folder
B. Create a page.js file inside the route folder
C. Create a _app.js file in the root folder
D. Create a server.js file in the route folder

Solution

  1. Step 1: Identify layout file naming in App Router

    Layouts are defined by creating a layout.js file inside the folder representing the route.
  2. Step 2: Exclude old or unrelated files

    The _app.js file is used in the old pages router, not App Router. page.js defines pages, not layouts. server.js is unrelated.
  3. Final Answer:

    Create a layout.js file inside the route folder -> Option A
  4. Quick Check:

    layout.js = layout file [OK]
Hint: Layouts use layout.js, not page.js or _app.js [OK]
Common Mistakes:
  • Using _app.js instead of layout.js
  • Confusing page.js with layout.js
  • Trying to define layout in server.js
3. Given this folder structure in the app/ directory:
app/
  layout.js
  page.js
  dashboard/
    layout.js
    page.js

What happens when a user visits /dashboard?
medium
A. Only the root layout wraps the dashboard page, dashboard layout is ignored
B. Only the dashboard layout wraps the dashboard page, root layout is ignored
C. The root layout wraps the dashboard layout, which wraps the dashboard page
D. No layout is applied, only the dashboard page renders

Solution

  1. Step 1: Understand nested layouts in App Router

    Layouts in parent folders wrap layouts and pages in child folders. So root layout wraps dashboard layout, which wraps dashboard page.
  2. Step 2: Confirm layout application order

    Visiting /dashboard applies root layout first, then dashboard layout, then renders dashboard page inside.
  3. Final Answer:

    The root layout wraps the dashboard layout, which wraps the dashboard page -> Option C
  4. Quick Check:

    Layouts nest from root to child [OK]
Hint: Layouts nest from parent to child folders [OK]
Common Mistakes:
  • Thinking only one layout applies
  • Ignoring root layout when inside subfolder
  • Assuming layouts do not nest
4. You wrote this code in app/dashboard/page.js:
export default function DashboardPage() {
  return 

Dashboard

}

But when you visit /dashboard, you get a blank page. What is the most likely cause?
medium
A. The page.js file must be named index.js
B. The DashboardPage component must be a class component
C. You forgot to export the component as default
D. Missing layout.js in app/ folder

Solution

  1. Step 1: Verify root layout presence

    In Next.js App Router, a root layout.js file in the app/ directory is required to render any pages, including nested ones.
  2. Step 2: Confirm naming and export correctness

    The page.js file name is correct, and the component is exported as default. Class components are not required.
  3. Final Answer:

    Missing layout.js in app/ folder -> Option D
  4. Quick Check:

    Root layout required for all pages [OK]
Hint: Root layout.js required for nested pages [OK]
Common Mistakes:
  • Renaming page.js to index.js incorrectly
  • Forgetting default export
  • Thinking class components are required
5. You want to create a nested route /blog/[slug] using the App Router. Which folder and file structure is correct?
hard
A. app/blog/_slug/page.js
B. app/blog/[slug]/page.js
C. app/blog/slug/page.js
D. app/blog/[slug].js

Solution

  1. Step 1: Understand dynamic route syntax in App Router

    Dynamic routes use square brackets around the parameter name as folder names, e.g., [slug].
  2. Step 2: Confirm correct file placement

    The dynamic folder [slug] contains a page.js file to render the dynamic route. Other options use incorrect naming or file placement.
  3. Final Answer:

    app/blog/[slug]/page.js -> Option B
  4. Quick Check:

    Dynamic routes use [param] folder with page.js [OK]
Hint: Dynamic routes use [param] folders with page.js inside [OK]
Common Mistakes:
  • Using _slug instead of [slug]
  • Placing dynamic param in filename instead of folder
  • Omitting page.js inside dynamic folder
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard