React Forum
Blog & Guide Responses
Dec 24, 2022 · 04:00 AM

MDN URLSearchParams - discussion (2022-12-24)

Started by Avery Chen
externalmdnurlsearchparamsroutingquerystate

URLSearchParams is one of those APIs that looks trivial until you try to use it as part of your routing story. We have a bunch of screens where the query is effectively route state (filters, sort, tab) and I keep seeing teams invent their own parsing conventions.

Read the docs

Do you treat search params as a first-class route contract, or as a convenience for shareable links? How do you encode complex state without turning URLs into unreadable blobs? What is your strategy for keeping query parsing consistent across file routes and component routes?

Comments (10)

Back to latest
Maya Henson
Dec 24, 2022 · 04:06 AM

If filters matter, the URL is the contract. Otherwise you cannot reproduce issues.

Dmitri Kline
Dec 24, 2022 · 04:15 AM

We standardized on a tiny codec layer per route so parsing is not scattered.

Related: Routing and Navigation.

Grace McCoy
Dec 24, 2022 · 04:23 AM

Simple pattern: keep keys stable and store derived evidence so you can render it in the shell.

ts
export function readFilters(search: string) {
  const p = new URLSearchParams(search);
  return { tab: p.get('tab') ?? 'all', sort: p.get('sort') ?? 'recent' };
}
Keira Santos
Dec 24, 2022 · 04:31 AM

Counterpoint: I think too many apps over-index on putting state in the URL.

It is useful for shareability, but it can become a leaky persistence layer.

Avery Chen
Dec 24, 2022 · 04:36 AM

I agree for deep internal UI state. For filter/search/sort, the URL has been a win for us.

The part that failed was inconsistency: every route used different key names and different parsing rules.

Keira Santos
Dec 24, 2022 · 04:40 AM

That distinction is fair: URL for the user-visible contract, not for every internal toggle.

Jules Kwon
Dec 24, 2022 · 04:49 AM

If you want it to stay sane, do not encode arbitrary JSON in the query.

Make the route own a small vocabulary of keys and value shapes.

Priya Liao
Dec 24, 2022 · 05:01 AM

We also do a small normalization step so support can read links.

For example, we map synonyms (new -> recent) instead of breaking old links.

Hana Saito
Dec 24, 2022 · 05:12 AM

A misunderstanding I had: I assumed URLSearchParams preserves type.

Once you accept everything is string, you stop trying to be clever and just define a route codec.

Salma Qureshi
Dec 24, 2022 · 05:24 AM

We render the parsed filter state as evidence in internal builds:

tsx
return <section data-tab={tab} data-sort={sort} />;

It sounds silly, but it made debugging link-driven state a lot faster.

Benji Rios
Dec 24, 2022 · 05:37 AM

Once you treat query params as route posture, the rest is just naming and normalization.

Morgan Patel
Dec 24, 2022 · 05:51 AM

I like MDN here because it reminds you what the platform guarantees and what it does not.

The rest is your routing story.