#Get Search Params from Localstorage before loading a page
21 messages · Page 1 of 1 (latest)
Is it possible to get this data from localstorage and set proper params?
sure, you just need to store them there
what is your actual question?
How to retrieve them and where? I'd like to do this operation inside createFileRoute, not at <Link /> level. I'm not sure whether it should be done in loader, beforeLoad or where exactly?
The case is that users use mostly the same set of filters every time, so I'd like to save them in localstorage and apply them every time they go to this page
Sorry if it's stupid question
I've tried to retrieve these filters in loader, then use them in my async function queryClient.ensureQueryData(getData(...)) , but these filters are obviously not set in the URL as search params
not at all, but your inital question was just a bit vague
you could do it in validateSearch
there you could check if a param is set, if not try to load it from localstorage and return it
here is a VERY simplistic implementation:
https://stackblitz.com/edit/tanstack-router-3ppts4?file=src%2Froutes%2Fabout.tsx&preset=node
thank you for this example. I've tried to do this like that before, but I abandoned it for some reason. As far as I understand, in validateSearch we kinda override default params, however we don't set them in URL at that time. I guess it's probably not possible to set these search params even before we call validateSearch. Your solution seems working, but I have some issues when reloading this page: Warning: Error in route match: *here is something like /my-page{"foo": "123"}* and this crashes the page
I will try to recreate this issue in stackblitz later
Having hard time recreating this issue, but looks like it starts here: Uncaught TypeError: Cannot read properties of undefined (reading 'queryClient'). And this happens when I go to this page (at this point URL has no search params) and refresh it (after refreshing params appear)
are you reading queryClientfrom context?
exactly
yeah we currently have an issue with context sometimes being undefined
if you can make this reproducable, please create a github issue for this
we need to track all of these context issues down
ok, I will do my best to recreate it
it might be the same thing as this here: https://github.com/TanStack/router/discussions/1839
I see that all my params are defined in validateSearch when this error happens