Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Q: How to replace createSearchParams #30

Closed
tordans opened this issue Feb 24, 2024 · 2 comments
Closed

Q: How to replace createSearchParams #30

tordans opened this issue Feb 24, 2024 · 2 comments

Comments

@tordans
Copy link
Contributor

tordans commented Feb 24, 2024

In https://github.com/nanostores/router/blob/main/CHANGELOG.md?plain=1#L10 I read that

  • Removed createSearchParams in favor of route.search of normal router.

I have this Astro App which uses export const $searchParams = createSearchParams() to create a store that is stored in the URL. https://github.com/osmberlin/www.osm-verkehrswende.org/blob/main/src/components/BaseMap/store.ts#L23

How would I migrate that to route.search? I am not using the router in Astro, I am looking for something like an atom, but in the URL.

@tordans tordans changed the title How to replace How to replace createSearchParams Feb 24, 2024
@tordans tordans changed the title How to replace createSearchParams Q: How to replace createSearchParams Feb 24, 2024
@ai
Copy link
Member

ai commented Feb 25, 2024

There are two options:

  1. Copy createSearchParams() implementation to your project. It is very simple store, you don’t to need some official library for that. Or you can create a support this library.
  2. Use normal createRouter() (you can use single route if you don’t need pathname routing) and read value.search of the router store.

tordans added a commit to osmberlin/www.osm-verkehrswende.org that referenced this issue Apr 3, 2024
@tordans
Copy link
Contributor Author

tordans commented Apr 3, 2024

Thanks for the hints @ai.

I tried suggestion (2) today but that is not ideal for this use case because it requires to specify the path of each route which uses my map component because the component needs to use openPage($router, 'NAME', undefined, { ...newParams }). I really just want to use the search params just like any other store without thinking about the URL and path.

So I went with suggestion (1) and copied the version 0.13.0 code in osmberlin/www.osm-verkehrswende.org@3d2e27b (with some minor TS adjustments).
It looks like this works fine.

I with there was something like https://github.com/47ng/nuqs for Astro/Plain React which work great in this use case where one wants to use a global state but store it in the URL.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants