Bird
Raised Fist0
NextJSframework~10 mins

Dynamic routes with [param] 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 dynamic route file named correctly for a parameter called 'id'.

NextJS
pages/[1].js
Drag options to blanks, or click blank then click option'
A[id]
Bid
C{id}
D(id)
Attempts:
3 left
💡 Hint
Common Mistakes
Using curly braces or parentheses instead of square brackets.
Naming the file simply 'id.js' which is a static route.
2fill in blank
medium

Complete the code to extract the dynamic route parameter 'id' using Next.js useRouter hook.

NextJS
import { useRouter } from 'next/router';

export default function Page() {
  const router = useRouter();
  const [1] = router.query.id;
  return <p>ID is: {id}</p>;
}
Drag options to blanks, or click blank then click option'
Aparam
Broute
Cquery
Did
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name different from 'id' causing confusion.
Trying to destructure router.query directly without .id.
3fill in blank
hard

Fix the error in the code to correctly display the dynamic route parameter 'slug'.

NextJS
import { useRouter } from 'next/router';

export default function Post() {
  const router = useRouter();
  const slug = router.query.[1];
  return <h1>Post: {slug}</h1>;
}
Drag options to blanks, or click blank then click option'
Aparam
Bslug
Cid
Dquery
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'id' or other names that don't match the route parameter.
Trying to access router.query directly without the parameter name.
4fill in blank
hard

Fill both blanks to create a dynamic route page that fetches the 'userId' parameter and displays it.

NextJS
import { useRouter } from 'next/router';

export default function User() {
  const router = useRouter();
  const [1] = router.query.[2];
  return <div>User ID: {userId}</div>;
}
Drag options to blanks, or click blank then click option'
AuserId
Bid
Dparam
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for the variable and query property.
Using generic names like 'id' when the parameter is 'userId'.
5fill in blank
hard

Fill all three blanks to create a dynamic route page that uses useRouter to get 'postId', checks if it's loading, and displays it.

NextJS
import { useRouter } from 'next/router';

export default function Post() {
  const router = useRouter();
  const [1] = router.query.[2];

  if (router.[3]) {
    return <p>Loading...</p>;
  }

  return <h1>Post ID: {postId}</h1>;
}
Drag options to blanks, or click blank then click option'
ApostId
CisFallback
DisLoading
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'isLoading' instead of 'isFallback' for loading state.
Using different names for the variable and query property.

Practice

(1/5)
1. What does the file name [id].js inside the pages folder in Next.js represent?
easy
A. A static page named literally 'id'
B. A dynamic route where id is a variable part of the URL
C. A special API route for handling requests
D. A CSS module file for styling

Solution

  1. Step 1: Understand Next.js dynamic route naming

    Files with names in square brackets like [id].js represent dynamic routes where the part inside brackets is a variable.
  2. Step 2: Interpret the meaning of [id].js

    This means the URL can have any value in place of id, and the page will render accordingly.
  3. Final Answer:

    A dynamic route where id is a variable part of the URL -> Option B
  4. Quick Check:

    Dynamic route = [param] file name [OK]
Hint: Square brackets mean dynamic URL part [OK]
Common Mistakes:
  • Thinking it's a static page named 'id'
  • Confusing with API routes
  • Assuming it's a styling file
2. Which of the following is the correct way to access the dynamic parameter slug in a Next.js page component inside pages/[slug].js?
easy
A. const { slug } = props.params;
B. const slug = params.slug;
C. const slug = useRouter().query.slug;
D. const slug = window.location.slug;

Solution

  1. Step 1: Recall how to get dynamic params in Next.js client-side

    Next.js provides the useRouter hook to access route parameters via router.query.
  2. Step 2: Identify correct syntax

    The correct way is const slug = useRouter().query.slug; to get the dynamic slug value.
  3. Final Answer:

    const slug = useRouter().query.slug; -> Option C
  4. Quick Check:

    useRouter().query.param gets dynamic route param [OK]
Hint: Use useRouter().query.param for dynamic params [OK]
Common Mistakes:
  • Trying to destructure from props.params directly
  • Using window.location which doesn't have slug
  • Using undefined variable params
3. Given the file pages/blog/[postId].js with this code:
import { useRouter } from 'next/router';
export default function Post() {
  const router = useRouter();
  return <div>Post ID: {router.query.postId}</div>;
}

What will be rendered when the user visits /blog/42?
medium
A. Post ID: postId
B. Error: router is undefined
C. Post ID: undefined
D. Post ID: 42

Solution

  1. Step 1: Understand dynamic route matching

    The file [postId].js matches URLs like /blog/42 and sets postId to '42'.
  2. Step 2: Check how router.query.postId is used

    The component uses router.query.postId to display the dynamic value, so it will show '42'.
  3. Final Answer:

    Post ID: 42 -> Option D
  4. Quick Check:

    Dynamic param value shown = 42 [OK]
Hint: router.query.param shows URL dynamic value [OK]
Common Mistakes:
  • Expecting literal 'postId' string output
  • Assuming undefined because of initial render
  • Thinking router is not defined
4. You have a dynamic route file pages/product/[productId].js. The component tries to access params.productId directly but gets an error. What is the likely cause?
medium
A. The component should use useRouter().query.productId instead of params.productId
B. The file name should be productId.js without brackets
C. Dynamic routes do not support parameters
D. You must import params from 'next/params'

Solution

  1. Step 1: Identify how to access dynamic params in client components

    In Next.js, dynamic route params are accessed via useRouter().query in client components, not directly from params.
  2. Step 2: Understand why params.productId causes error

    Using params.productId without proper context or props causes undefined errors.
  3. Final Answer:

    The component should use useRouter().query.productId instead of params.productId -> Option A
  4. Quick Check:

    Use useRouter().query for dynamic params [OK]
Hint: Use useRouter().query, not params directly [OK]
Common Mistakes:
  • Trying to access params without props or hooks
  • Renaming file incorrectly
  • Expecting automatic import of params
5. You want to create a dynamic route in Next.js that matches URLs like /user/123/profile. Which folder and file structure inside app directory correctly implements this using dynamic segments?
hard
A. app/user/[id]/profile/page.js
B. app/user/profile/[id].js
C. app/user/[id]-profile/page.js
D. app/user/[id]/profile.js

Solution

  1. Step 1: Understand Next.js app router folder structure

    Each folder represents a URL segment. Dynamic segments use square brackets like [id].
  2. Step 2: Map URL /user/123/profile to folders

    The URL has three parts: 'user', dynamic '123', and 'profile'. So folders should be user/[id]/profile with a page.js inside.
  3. Final Answer:

    app/user/[id]/profile/page.js -> Option A
  4. Quick Check:

    Dynamic folder inside URL path = [param] folder [OK]
Hint: Dynamic segments are folders with [param] in app router [OK]
Common Mistakes:
  • Placing dynamic param after static folder incorrectly
  • Using dash instead of folder for dynamic segment
  • Putting dynamic param in wrong folder level
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