📖 Scenario: You are building a simple product filter page in Next.js. The page should read a category from the URL's search parameters and show it on the page. This helps users share filtered views by URL.
🎯 Goal: Create a Next.js functional component that reads the category search parameter from the URL and displays it. The component should update if the URL changes.
📋 What You'll Learn
Use Next.js App Router with a functional component
Use the useSearchParams hook to read URL search parameters
Display the current category parameter value on the page
Update the displayed category if the URL changes
💡 Why This Matters
🌍 Real World
Many web apps use URL search parameters to keep track of filters or settings so users can share links with the current view.
💼 Career
Understanding how to sync UI state with URL parameters is important for building user-friendly, shareable web applications in Next.js.
Progress0 / 4 steps
1
Create a basic Next.js functional component
Create a functional component called CategoryFilter that returns a <div> with the text Filter page inside.
NextJS
Hint
Start with export default function CategoryFilter() { return <div>Filter page</div>; }
2
Import and use the useSearchParams hook
Import useSearchParams from next/navigation and call it inside CategoryFilter to get the search parameters object called searchParams.
NextJS
Hint
Use import { useSearchParams } from 'next/navigation' and then const searchParams = useSearchParams() inside the component.
3
Read the category parameter from the URL
Use searchParams.get('category') to get the category value from the URL and store it in a variable called category inside the CategoryFilter component.
NextJS
Hint
Use const category = searchParams.get('category') to read the category parameter.
4
Display the category value dynamically
Update the returned JSX to show the text Selected category: followed by the category variable inside a <p> tag. If category is null, show No category selected instead.
NextJS
Hint
Use JSX with {category ?? 'No category selected'} inside a <p> tag.
Practice
(1/5)
1. What does the useSearchParams hook in Next.js App Router do?
easy
A. It fetches data from an API based on URL parameters.
B. It changes the URL path without reloading the page.
C. It reads and lets you access query parameters from the URL.
D. It manages local component state unrelated to the URL.
Solution
Step 1: Understand the purpose of useSearchParams
The hook is designed to read query parameters from the URL, making it easy to access values like ?page=2 or ?filter=active.
Step 2: Compare with other options
Changing URL path or fetching data are different tasks; useSearchParams specifically reads URL search parameters.
Final Answer:
It reads and lets you access query parameters from the URL. -> Option C
Quick Check:
useSearchParams reads URL query parameters = C [OK]
Hint: Remember: useSearchParams reads URL queries, not changes path [OK]
Common Mistakes:
Confusing useSearchParams with navigation or data fetching hooks
Thinking it changes the URL instead of reading it
Assuming it manages local state unrelated to URL
2. Which of the following is the correct way to import and use useSearchParams in a Next.js component?
easy
A. import { useSearchParams } from 'next/navigation'; const params = useParams();
B. import useSearchParams from 'next/router'; const params = useSearchParams();
C. import { useSearchParams } from 'next/router'; const params = useSearchParams();
D. import { useSearchParams } from 'next/navigation'; const params = useSearchParams();
Solution
Step 1: Identify correct import source
In Next.js App Router, useSearchParams is imported from 'next/navigation', not 'next/router'.
Step 2: Check usage syntax
The hook is called as useSearchParams(), returning the params object. Using useParams() is incorrect here.
Final Answer:
import { useSearchParams } from 'next/navigation'; const params = useSearchParams(); -> Option D
Quick Check:
Correct import and call = D [OK]
Hint: Import from 'next/navigation' and call useSearchParams() [OK]
Common Mistakes:
Importing from 'next/router' instead of 'next/navigation'
Calling useParams() instead of useSearchParams()
Using default import instead of named import
3. Given this Next.js component code snippet, what will be logged if the URL is ?page=3&sort=asc?
import { useSearchParams } from 'next/navigation';
export default function Page() {
const searchParams = useSearchParams();
console.log(searchParams.get('page'));
console.log(searchParams.get('sort'));
return null;
}
medium
A. 3 and asc
B. page and sort
C. null and null
D. undefined and undefined
Solution
Step 1: Understand useSearchParams behavior
useSearchParams returns an object with a .get() method to read query parameters by name.
Step 2: Check the URL query parameters
The URL has ?page=3 and ?sort=asc, so searchParams.get('page') returns '3' and searchParams.get('sort') returns 'asc'.
A. The import statement is incorrect; it should be from 'next/router'.
B. useSearchParams is not called as a function, so params is the function itself.
C. The component returns invalid JSX syntax with <div> tags.
D. The get method does not exist on the params object.
Solution
Step 1: Check how useSearchParams is used
The code assigns params = useSearchParams without parentheses, so params is the function itself, not the returned object.
Step 2: Understand consequence of missing parentheses
Calling params.get('filter') fails because params is a function, not the searchParams object with .get() method.
Final Answer:
useSearchParams is not called as a function, so params is the function itself. -> Option B
Quick Check:
Missing () after useSearchParams causes error = A [OK]
Hint: Always call useSearchParams() with parentheses [OK]
Common Mistakes:
Forgetting parentheses when calling hooks
Importing from wrong module
Misunderstanding JSX syntax for div
5. You want to build a Next.js page that shows a list filtered by a search term from the URL query parameter search. Which approach correctly updates the displayed list when the URL changes without a full page reload?