Using URL state with searchParams helps keep track of information in the web address. This makes it easy to share or save the current page view.
URL state with searchParams in NextJS
import { useSearchParams } from 'next/navigation'; const searchParams = useSearchParams(); const value = searchParams.get('key');
useSearchParams is a React hook from Next.js App Router to read URL query parameters.
It returns a URLSearchParams object to get values by key.
page parameter from the URL.const searchParams = useSearchParams(); const page = searchParams.get('page');
filter parameter or use all if not present.const searchParams = useSearchParams(); const filter = searchParams.get('filter') || 'all';
q.const searchParams = useSearchParams(); const searchTerm = searchParams.get('q');
This component reads the query parameter from the URL and shows it on the page. If no query is in the URL, it shows an empty string.
'use client'; import React from 'react'; import { useSearchParams } from 'next/navigation'; export default function SearchPage() { const searchParams = useSearchParams(); const query = searchParams.get('query') || ''; return ( <main> <h1>Search Page</h1> <p>Showing results for: <strong>{query}</strong></p> </main> ); }
Changing searchParams does not automatically update the component. You need to navigate or refresh to see changes.
Use Next.js navigation methods to update URL parameters and keep UI in sync.
URL state with searchParams lets you read query parameters from the URL easily.
This helps keep the page state shareable and reloadable.
Use useSearchParams hook in Next.js App Router to access these values.
Practice
useSearchParams hook in Next.js App Router do?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 CQuick Check:
useSearchParams reads URL query parameters = C [OK]
- 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
useSearchParams in a Next.js component?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 DQuick Check:
Correct import and call = D [OK]
- Importing from 'next/router' instead of 'next/navigation'
- Calling useParams() instead of useSearchParams()
- Using default import instead of named import
?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;
}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'.Final Answer:
3 and asc -> Option AQuick Check:
searchParams.get('page') = '3', get('sort') = 'asc' [OK]
- Expecting keys instead of values
- Confusing null with undefined for missing params
- Not calling .get() method on searchParams
import { useSearchParams } from 'next/navigation';
export default function Page() {
const params = useSearchParams;
const filter = params.get('filter');
return <div>Filter: {filter}</div>;
}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 BQuick Check:
Missing () after useSearchParams causes error = A [OK]
- Forgetting parentheses when calling hooks
- Importing from wrong module
- Misunderstanding JSX syntax for div
search. Which approach correctly updates the displayed list when the URL changes without a full page reload?
import { useSearchParams } from 'next/navigation';
import { useEffect, useState } from 'react';
export default function SearchPage() {
const searchParams = useSearchParams();
const [searchTerm, setSearchTerm] = useState('');
// Which code below correctly updates searchTerm when URL changes?
// A:
// useEffect(() => {
// setSearchTerm(searchParams.get('search') || '');
// }, [searchParams]);
// B:
// setSearchTerm(searchParams.get('search') || '');
// C:
// useEffect(() => {
// setSearchTerm(searchParams.search || '');
// }, [searchParams]);
// D:
// useEffect(() => {
// setSearchTerm(searchParams.get('searchTerm') || '');
// }, [searchParams]);
return <div>Search: {searchTerm}</div>;
}Solution
Step 1: Understand reactivity to URL changes
useSearchParams returns a new object when URL changes, so useEffect with [searchParams] dependency runs on URL updates.Step 2: Correctly read the query parameter
The parameter key is 'search', so use searchParams.get('search'). Accessing searchParams.search or wrong keys will fail.Final Answer:
Use useEffect with dependency on searchParams and call searchParams.get('search'). -> Option AQuick Check:
useEffect + searchParams.get('search') updates state on URL change = B [OK]
- Calling setState outside useEffect causing infinite loops
- Accessing non-existent properties on searchParams
- Using wrong query parameter keys
Ready to prove it?
Pass the test and this pattern moves unknown
