React Easy technical 0 views 2 min read

How do query parameters work?

Peer-reviewed by HireXTech Technical Panel Updated for 2025/2026 hiring Editorial standards
Practise this track
Interviewer Expectations for this Question
01
Core Competency

Assesses fundamental understanding of React conventions, runtime behavior, and memory/performance considerations.

02
Evaluation Criteria

Hiring managers look for precision, avoidance of ambiguous jargon, and ability to explain trade-offs under real production conditions.

Comprehensive Model Answer Verified Solution

Query (search) parameters are the ?key=value&key2=value2 portion of a URL. Unlike route params (see [What are dynamic routes and what are route parameters?](#what-are-dynamic-routes-and-what-are-route-parameters)), they aren't part of the route's path definition — they're read and updated with the useSearchParams hook, which mirrors the browser's URLSearchParams API.

     import { useSearchParams } from "react-router-dom";

     function ProductList() {
       // URL: /products?category=shoes&sort=price&page=2
       const [searchParams, setSearchParams] = useSearchParams();

       const category = searchParams.get("category"); // "shoes"
       const sort = searchParams.get("sort");           // "price"
       const page = Number(searchParams.get("page") ?? "1"); // params are always strings

       function handleSortChange(nextSort) {
         // Updates the URL (?sort=...) and triggers a re-render with the new value
         setSearchParams((prev) => {
           prev.set("sort", nextSort);
           return prev;
         });
       }

       return (
         <div>
           <select value={sort ?? ""} onChange={(e) => handleSortChange(e.target.value)}>
             <option value="price">Price</option>
             <option value="rating">Rating</option>
           </select>
           {/* render products filtered by category, sorted by sort, paginated by page */}
         </div>
       );
     }
     

Key points:

  • useSearchParams() returns [searchParams, setSearchParams], similar to useStatesearchParams is a read-only URLSearchParams instance (.get(), .getAll(), .has()), and setSearchParams() updates the URL (and re-renders).
  • Updating search params pushes a new history entry by default; pass { replace: true } as a second argument to setSearchParams to avoid cluttering back/forward history (useful for things like live search-as-you-type).
  • Query params are ideal for optional, non-hierarchical, shareable UI state — filters, sorting, pagination, search text — because the resulting URL can be bookmarked/shared and still reproduces the same view.
  • Unlike route params, missing query params don't cause a 404/no-match; searchParams.get("missing") simply returns null, so always provide sensible defaults.

## Old Q&A

Candidate Response Strategy & Interview Tips

  1. Start with a concise one-sentence summary: Deliver a direct, confident answer first before expanding into nuances.
  2. Demonstrate real-world trade-offs: Discuss where this approach excels and when you would avoid it in production systems.
  3. Discuss complexity & edge cases: Proactively explain time/space complexity or boundary conditions (null values, scale limits).
  4. Prepare for interviewer follow-ups: Technical hiring panels frequently probe deeper into concurrency, backward compatibility, or alternative libraries.
Related Topics & Skills
Spotted an error or have an alternative solution?