tanstack router + query + searchParams = keepPreviousData does not work
Hey 👋🏽 ,
thanks for this great project. I'm using
tanstack/router and tanstack/query. The filter + pagination state should be in searchParams. At the query I use keepPreviousData: true, but if a page is not in cache, I get a loading-state. What I'm doing wrong?
The page switch is done via a navigation:
Thanks9 Replies
correct-apricot•3y ago
you're not showing
useParticipantsQuery. a codesandbox would be bestrising-crimsonOP•3y ago
Thanks for the response. Here's the
useParticipantsQuery. Is the issue, that the ParticipantsPage is rerendered after the navigation? If yes, do you know a workaround?
correct-apricot•3y ago
well
keepPreviousData only works if a page re-renders and the observer previously had data. if you mount a new page, there is no "previous data" to keeprising-crimsonOP•3y ago
So searchParams cannot be changed without remounting the page?
correct-apricot•3y ago
I don't know ?
rising-crimsonOP•3y ago
Ok, thanks!
@TkDodo 🔮 Maybe the way to go is to use the
load ft of the route. What do you think about it?
ensureQueryData needs also a queryFn-prop. In my case, I only want to ensure data if the query already exists. I therefore do not want to provide a queryFn. Is that possible?correct-apricot•3y ago
In my case, I only want to ensure data if the query already exists.what does that mean? "ensure" means to make sure that something exists. If the query doesn't exist yet,
ensureQueryData will fetch so that after you await ensureQueryData(), you can be sure that data is in the cache 😅
if the query already exsists, it just reads that from the cache. From what you are describing, it sounds like you want the normal default behaviour of useQuery without any loaders ...
if the loaders don't load data for queries that don't yet exist, what are they doing? I think I'm just misunderstanding you thoughrising-crimsonOP•3y ago
I understand what you mean.
I am currently at a crossroads as to whether to keep the filterParams as searchParams or to store them in
zustand. SearchParams sounds tempting, as it improves the UX considerably (browser histroy), but the DX is still lower at the moment (at least for me). Remounting the page on every filtering/page-change feels inefficient. Or is that pretty much irrelevant in React? Maybe I lack internal knowledge to answer this question. Do you have any input?
What I want to achieve here is simply the great tanstack-query-pagination-behavior (keepPreviousData) that I can't manage with the SearchParams.rising-crimsonOP•3y ago
@TkDodo 🔮 I took another close look. It seems to work now. Checkout: https://stackblitz.com/edit/tanstack-router-ni9orz?file=src%2Fmain.tsx
My mistake was using
keepPreviousData in v5 🙈🙈🙈🙈🙈🙈🙈🙈Armin
StackBlitz
pagination-with-search-params - StackBlitz
Run official live example code for Router Basic, created by Tanstack on StackBlitz