# Query Strings (useSearchParams)

> Reading URL parameters like ?page=2 or ?sort=asc.

Subject: Next.js | Lesson 40 of 65 | 5 min read
Interactive lesson: https://codewithanimation.in/nextjs/use-search-params
Previous: [Active Links (usePathname)](https://codewithanimation.in/nextjs/use-pathname.md) | Next: [Dynamic Segments (useParams)](https://codewithanimation.in/nextjs/use-params.md)

## Shareable State

`useSearchParams` is a Client Component hook that lets you read the query string of the current URL.

Storing state in the URL (like search filters or pagination) is a best practice because it allows users to bookmark and share exact views of your application.

## The Read-Only API

The hook returns a read-only version of the standard `URLSearchParams` web API.

It provides utility methods like `get('key')`, `getAll('key')`, and `has('key')` to easily extract values without manually parsing strings.

## Updating Parameters

`useSearchParams` only *reads* the parameters.

To update them, you must create a new `URLSearchParams` object, modify it, and pass it to `useRouter().push()`. There is no `setSearchParams` hook.

## Common mistake: The Suspense Trap (De-opting)

Because search parameters are not known during the build process, using `useSearchParams` will force Next.js to dynamically render the route on every request, destroying your static caching.

To prevent this, you MUST wrap any component that calls `useSearchParams` inside a `<Suspense>` boundary. This allows the server to statically render the rest of the page, and only fill in the query-dependent parts on the client.

## Tip: Server Components

You cannot use `useSearchParams` in a Server Component.

Instead, Next.js passes a `searchParams` prop directly to Server Components (`page.tsx`).

## Try it: The Filter Flow

Click the color filters below to modify the URL. Watch how the mock hook reacts to the URL change, and note the Suspense Boundary warning.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/nextjs/use-search-params

## Check yourself

### Question 1

What happens if you use `useSearchParams` without wrapping the component in a `<Suspense>` boundary?

- A. The app will crash with a React Hook error.
- B. The hook will return `null`.
- C. Next.js will de-opt the route into dynamic rendering, breaking static caching.
- D. The search parameters will be stripped from the URL.

Correct answer: C

Next.js will warn you during the build that the route was forced into dynamic rendering. Always wrap `useSearchParams` in `<Suspense>` to preserve Static Generation.

- Why not A: It works, but with a major performance penalty.
- Why not B: It returns the params.
- Why not D: The URL remains untouched.

### Question 2

How do you update a search parameter using this hook?

- A. You can't. You must use `useRouter` to navigate to a new URL.
- B. `searchParams.set('key', 'value')`
- C. `const [params, setParams] = useSearchParams()`
- D. `searchParams.update({ key: 'value' })`

Correct answer: A

`useSearchParams` is strictly read-only. To change the URL, you must construct a new URL string and pass it to `router.push()`.

- Why not B: The hook returns a read-only object.
- Why not C: There is no setter.
- Why not D: This method does not exist.

## Remember this

- `useSearchParams` reads the query string (e.g., `?sort=asc`).
- It returns a READ-ONLY object. You cannot update the URL directly with it.
- Using it forces dynamic rendering UNLESS you wrap the component in `<Suspense>`.
- Server Components should use the `searchParams` prop instead of this hook.
