-
Notifications
You must be signed in to change notification settings - Fork 4
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
Suggestion: useSearchParamState #9
Comments
Thanks for another great suggestion @iamnafets! Could you elaborate a bit more over how would this be different/better than using: export const { routes, useSafeParams, useSearchParamsState } = createNavigationConfig(defineRoute => ({
search: defineRoute('/search', { search: z.object({ query: z.string(), page: z.coerce.number() }) })
}));
// Search Page
export default function SeachPage() {
// ...
return (
<Pagination>
<Link href={routes.search({ search: { query, page: page + 1 } })}>Next page</Link>
</Pagination>
)
} |
For SPAs, you often want to manipulate the querystring without navigating. React's |
How come? I mean, what's the point of storing state in the URL if you're not navigating? |
There are quite a few situations where you'd want to do this. We use it for query parameters for filtering a table. The general idea is that by having state reflected in the URL, you can make your URLs much more useful for sharing purposes. There's a great lib, nuqs (https://nuqs.47ng.com/), which I use extensively for this. Actually, I came here to suggest considering integration with |
Yes, I get the point of having state reflect in the URL but I strongly disagree that there are any situations where a query parameter changing doesn't reflect on browser navigation |
Well, this is precisely how nextJS used to work: https://nextjs.org/docs/pages/building-your-application/routing/linking-and-navigating#shallow-routing This is really a semantic issue. Changing the query does reflect on "browser navigation" - but only on first load. After that, updates can optionally be done "client first", so that you can use query parameters as direct replacements for |
I think one difference might be that a setSearchParams({ page: page + 1 }); vs routes.search({
page: page + 1,
query,
sort
// etc
}); But I believe this could be built on top of this library. |
Sometimes it's useful to use the search params to store state. We currently have something like this:
Where
setQuery
actually changes the search params and updates the state. I'd love to incorporate this into the library. Any interest?Thinking this look like:
The text was updated successfully, but these errors were encountered: