How to access query client instance outside of the router's context?
The generated project creates an instance of query client in the getRouter func and adds it to the context so that we can access it in loader, etc. How can i access this instance from somewhere else?
5 Replies
other-emerald•2mo ago
context is passed to your loader function param
rival-black•2mo ago
what is this somewhere else? because if it is within react component you can use the query provider to get the query client, (useQueryClient() hook), outside of react, you can make sure to have a singleton instance of query client per request, so it all depends on where you trying to get the query client
rival-blackOP•2mo ago
I want to integrate with tanstack db. I need to create a collection and export it. It requires a query client instance.
Can you guide me how to make a singleton instance per request? I am not familiar with the life cycle in tanstack start.
fascinating-indigo•2mo ago
@grth check that => https://github.com/CarlosZiegler/fullstack-start-template/blob/main/src/router.tsx
I added Provider into Wrap on createRouter, that is a way to wrap your app with tanstack provider and able you to use that.
GitHub
fullstack-start-template/src/router.tsx at main · CarlosZiegler/fu...
This project provides a solid foundation for building modern web applications using a curated stack of technologies focused on developer experience, performance, and type safety. - CarlosZiegler/fu...

vicious-gold•2mo ago
do you do this once? then do it in getRouter()
otherwise, you can access the router instance via
getRouterInstance()and then access its context