2022-09-09
1309
#react
John Reilly
131517
Sep 9, 2022 ⋅ 4 min read

useState with URLs: How to persist state with useSearchParams

John Reilly MacGyver turned Dev 🌻❤️ TypeScript / ts-loader / fork-ts-checker-webpack-plugin / DefinitelyTyped: The Movie

Recent posts:

How to use the Swiper.js slider library

Learn how to use Swiper, a powerful JavaScript slider library, to create responsive, interactive, and smooth UI experiences for websites.

Jude Miracle
Mar 18, 2025 ⋅ 14 min read
A Complete Guide to Fetch API In Javascript

A complete guide to Fetch API in JavaScript

Learn how to use the Fetch API, an easy method for fetching resources from a remote or local server through a JavaScript interface.

Njong Emy
Mar 17, 2025 ⋅ 8 min read
typescript enums

TypeScript enums: Usage, advantages, and best practices

Learn how TypeScript enums work, the difference between numeric and string enums, and when to use enums vs. other alternatives.

Clara Ekekenta
Mar 14, 2025 ⋅ 7 min read
how to handle react-scripts in a fast-changing React landscape

How to handle react-scripts in a fast-changing React landscape

Review the basics of react-scripts, its functionality, status in the React ecosystem, and alternatives for modern React development.

Ibrahima Ndaw
Mar 13, 2025 ⋅ 9 min read
View all posts

5 Replies to "<code>useState</code> with URLs: How to persist state with <code>useSearchParams</code>"

  1. You can update search params easily with this approach, using native useSearchParams functional update.

    setSearchParams((searchParams) => {
    searchParams.set(“greeting”, “hi”);
    return searchParams;
    });

  2. Hi,

    Great post!
    It would be nice to see a section on how to manage updating multiple keys at the same time.
    For example if you set State A then State B you will end up with only State B’s changes.

    In my case I am trying to update a start and end date that are updated by the same callback function.

    Cheers,
    Casey

Leave a Reply