Hacker News new | ask | show | jobs
by cadamsdotcom 329 days ago
This pattern - saving the query to the URL with the history API - is fantastic UX but never gets implemented because there’s never time. Luckily an LLM can build this quickly as it’s straightforward and mostly boilerplate.

Still the boilerplate makes me wonder if it belongs in a library, eg. a React hook that’s a drop in replacement for `useState`. Backend logic would still need to be implemented. Does something like this exist?

6 comments

> is fantastic UX but never gets implemented because there’s never time

Wouldn't the change take something like an hour the first time you implement it and then 10s of seconds for calling the centralized function henceforth?

I don't think the problem is "there's never time"; and if that is the problem, I don't think an LLM will "solve" that, especially since studies have shown developers are slower when they use LLMs to code for them.

Agreed I love this pattern! I'm a big fan of using nuqs for this in nextjs, but really stoked to try out rust / loco / htmx for my next project.
> never gets implemented because there’s never time

In my experience that time is saved and more when you find you no longer need to manage Zustand/redux stores to track application state. This pattern works beautifully when incorporating the query parameters as query keys with tan stack query too.

> Still the boilerplate makes me wonder if it belongs in a library, eg. a React hook that’s a drop in replacement for `useState`

That’s exactly what `nuqs` does (disclaimer: I’m the author).

> Backend logic would still need to be implemented

Assuming your backend is written in TypeScript, you can use nuqs loaders to reuse the same validation logic on both sides.

https://nuqs.47ng.com

At work we have a hook for that with various backends (memory, local storage, redux, and search params). Supports page, offset, and cursor pagination.

https://stackblitz.com/edit/github-8ssor8-rqkyew8w?file=src%...

Doesn't react-router do something like this?
Yep, `useSearchParams()`. At work I built a wrapper to incorporate zod schemas for typesafe search param state. Nuqs is the best for this if your application meets its prerequisites: https://nuqs.47ng.com/