T
TanStack5mo ago
flat-fuchsia

Get router search params imperatively one time

So I have form with about 10-15 fields and I’m trying synchronise form state values to the query params. I use routerApi.useSearch hook to get current search values on the first render and then I am managing that state in form only. To synchronise my form state with url search params I use onBlur. I can sync it in real time because when user editing it is very slow and sluggish, so that is why I updating search params only on blur. Problem is that while search params updating there is a bit delay when I click on another field before it is focused(on blur fires from prev field abs query params updating). Also my component rerenders because of useSearch hook(even though I do not need data from it) Is there imperative way to get current URL search params? Like routeAPI.getSearch() ? Is there any way to prevent my component from rerendering when search params change?
3 Replies
sunny-green
sunny-green5mo ago
Also my component rerenders because of useSearch hook(even though I do not need data from it)
dont use useSearch then see https://github.com/TanStack/router/discussions/1460#discussioncomment-12240039
flat-fuchsia
flat-fuchsiaOP5mo ago
Thank you! That seems exactly what I wanted! Thought type of router.state.location.search is different from what useSearch returns, for some reason it makes every field in object optional, this was not the case with useSearch, not sure which is it like that
sunny-green
sunny-green5mo ago
router.state.location.search does not "know" which route you are on while with useSearch you assert the route you could also use useMatches, find the match by routeId and then access its search

Did you find this page helpful?