Description
Is your feature request related to a problem? Please describe.
In our app we sometimes need to pass along a search
string, e.g. from window.location.search
. This works great with the Link
component since you can pass it to the href like this:
<Link href={{ pathname: '/about', search: window.location.search }}>
About
</Link>
But if you need to do programmatic routing using push
/replace
this requires you to pass it as a query
object instead (especially if you're using type-safe routing):
push({ pathname: 'about', query: { ... }})
I can't find any simple way of passing an already encoded search string to push
or replace
. Also it's a little bit unclear to me how the query
would be encoded. For example how could one customize how array values are encoded?
Describe the solution you'd like
It'd be great if a search
string could be passed to push
and replace
. This would also enable doing advanced patterns such as:
const searchParams = new UrlSearchParams({ returnTo: window.location.search })
push({ pathname: 'about', search: searchParams.toString() })
Describe alternatives you've considered
Allow passing URLSearchParams to the push
and replace
methods. Could make sense since that's a fairly well adopted standard. The URLSearchParams
standard has some problems though and it doesn't solve supporting different encodings of e.g. array values.
It would be nice if the typed pathnames always allowed suffixing with a search string or hash, like Next.js typedRoutes
support. This wouldn't work great with dynamic routes though (push('/[slug]?foo=bar')
doesn't really make sense)