r/reactjs • u/TkDodo23 • May 26 '26
Resource TanStack Router and Query
https://tkdodo.eu/blog/tan-stack-router-and-query๐ Surprisingly, I haven't written about how my two favourite TanStack libraries - Router and Query - work together. Well, here it is:
๐ loaders start fetches early
๐ฎ query owns the cache & keeps data alive
๐ suspense integrates naturally
๐๏ธ TanStack Start makes SSR & streaming easy
108
Upvotes
17
u/edvinerikson May 26 '26
Great tips, I use it the way you described here, but I struggle with keeping the loader preloading in sync with whatโs actually used in the page. Very easy to accidentally mess up query options so you load data twice. Also very easy to forget adding the preloading.
I wish for an api where you are forced to pass references from the router onto each useQuery to ensure they always match and always the same. Kind of like Relays useFragment/usePreloadedQuery APIs.
2
u/Suspicious-Name4273 May 27 '26
Afaik tanstack start is refetching loader queries on hydration, is that correct? If yes, is setting staleTime>0 the only way around this?
1
u/sondr3_ May 27 '26
What's a good way to handle prefetching for queries that also depend on search parameters? We have a few pages in our app that does server side filtering/sorting etc where the state is stored in the URL/persisted in
localStoragebetween visits to the app and then fetched on page load. Without having those available the prefetching is essentially wasted, so we ended up not using loaders until we find a better solution. I wrote a hacky search middleware that we used to read the state fromlocalStoragebefore callingnext, but when dropping loaders we dropped that as well.4
u/SeaEnergy264 May 27 '26
Not enough emojis ๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐
3
u/rsajdok May 26 '26
Thanks a lot ๐
3
u/well-litdoorstep112 May 27 '26
โจ๐๐ฌ Can you add more emojis? ๐ค๐๐๐ฅ๐โจ๐ฏ๐๐ฑ๐๐ฅณ๐ฅ๐ซถ๐๐๐๐ซ๐ต๐๐๐๐ชฉ๐ฏ๐๐๐ฃ๐๐ฅ
1
u/Brilla-Bose May 27 '26
you're the best! thank you!